import numpy as np
import pandas as pd
from statsmodels import robust
from scipy.stats import iqr
from scipy import stats
AAPL = pd.read_csv('AAPL.csv', index_col=0)
IBM = pd.read_csv('IBM.csv', index_col=0)
JPM = pd.read_csv('JPM.csv', index_col=0)
DJI = pd.read_csv('DJI.csv', index_col=0)
AAPL.index = pd.to_datetime(AAPL.index)
IBM.index = pd.to_datetime(IBM.index)
DJI.index = pd.to_datetime(DJI.index)
JPM.index = pd.to_datetime(JPM.index)
vals = ['Open', 'High', 'Low', 'Close', 'Adj Close', 'Volume']
statistics = ['Mean', 'Median', 'Standard Deviation', 'Median Absolute Deviation', 'Interquartile Range']
stats_df_final = pd.DataFrame(index=statistics, columns=vals)
apple_data = AAPL.copy()
for val in vals:
col = apple_data[val]
stats_df_final[val]['Mean'] = col.mean()
stats_df_final[val]['Median'] = col.median()
stats_df_final[val]['Standard Deviation'] = col.std()
stats_df_final[val]['Median Absolute Deviation'] = stats.median_abs_deviation(col)
stats_df_final[val]['Interquartile Range'] = iqr(col)
# stats_df_final.to_csv('AAPLstats.csv')
vals = ['Open', 'High', 'Low', 'Close', 'Adj Close', 'Volume']
statistics = ['Mean', 'Median', 'Standard Deviation', 'Median Absolute Deviation', 'Interquartile Range']
stats_df_final = pd.DataFrame(index=statistics, columns=vals)
ibm_data = IBM.copy()
for val in vals:
col = ibm_data[val]
stats_df_final[val]['Mean'] = col.mean()
stats_df_final[val]['Median'] = col.median()
stats_df_final[val]['Standard Deviation'] = col.std()
stats_df_final[val]['Median Absolute Deviation'] = stats.median_abs_deviation(col)
stats_df_final[val]['Interquartile Range'] = iqr(col)
# stats_df_final.to_csv('IBMstats.csv')
vals = ['Open', 'High', 'Low', 'Close', 'Adj Close', 'Volume']
statistics = ['Mean', 'Median', 'Standard Deviation', 'Median Absolute Deviation', 'Interquartile Range']
stats_df_final = pd.DataFrame(index=statistics, columns=vals)
dji_data = DJI.copy()
for val in vals:
col = dji_data[val]
stats_df_final[val]['Mean'] = col.mean()
stats_df_final[val]['Median'] = col.median()
stats_df_final[val]['Standard Deviation'] = col.std()
stats_df_final[val]['Median Absolute Deviation'] = stats.median_abs_deviation(col)
stats_df_final[val]['Interquartile Range'] = iqr(col)
# stats_df_final.to_csv('DJIstats.csv')
vals = ['Open', 'High', 'Low', 'Close', 'Adj Close', 'Volume']
statistics = ['Mean', 'Median', 'Standard Deviation', 'Median Absolute Deviation', 'Interquartile Range']
stats_df_final = pd.DataFrame(index=statistics, columns=vals)
jpm_data = JPM.copy()
for val in vals:
col = jpm_data[val]
stats_df_final[val]['Mean'] = col.mean()
stats_df_final[val]['Median'] = col.median()
stats_df_final[val]['Standard Deviation'] = col.std()
stats_df_final[val]['Median Absolute Deviation'] = stats.median_abs_deviation(col)
stats_df_final[val]['Interquartile Range'] = iqr(col)
# stats_df_final.to_csv('JPMstats.csv')
apple_data['Simple Returns'] = apple_data['Adj Close'].pct_change()
apple_data['Log Returns'] = np.log(apple_data['Adj Close']).diff()
ibm_data['Simple Returns'] = ibm_data['Adj Close'].pct_change()
ibm_data['Log Returns'] = np.log(ibm_data['Adj Close']).diff()
jpm_data['Simple Returns'] = jpm_data['Adj Close'].pct_change()
jpm_data['Log Returns'] = np.log(jpm_data['Adj Close']).diff()
dji_data['Simple Returns'] = dji_data['Adj Close'].pct_change()
dji_data['Log Returns'] = np.log(dji_data['Adj Close']).diff()
import matplotlib.pyplot as plt
apple_data['Simple Returns'].plot(color = 'blue')
plt.title('Simple Returns for Apple', fontname="arial", fontsize=14, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=12)
plt.ylabel('Simple Returns', fontname="arial", fontsize=12)
plt.xticks(fontname="arial", fontsize=10)
plt.yticks(fontname="arial", fontsize=10)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(6, 4)
# plt.savefig('4_1_1a.pdf')
apple_data['Log Returns'].plot(color = 'red')
plt.title('Log Returns for Apple', fontname="arial", fontsize=14, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=12)
plt.ylabel('Log Returns', fontname="arial", fontsize=12)
plt.xticks(fontname="arial", fontsize=10)
plt.yticks(fontname="arial", fontsize=10)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(6, 4)
# plt.savefig('4_1_1b.pdf')
ibm_data['Simple Returns'].plot(color = 'blue')
plt.title('Simple Returns for IBM', fontname="arial", fontsize=14, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=12)
plt.ylabel('Simple Returns', fontname="arial", fontsize=12)
plt.xticks(fontname="arial", fontsize=10)
plt.yticks(fontname="arial", fontsize=10)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(6, 4)
plt.savefig('4_1_1c.pdf')
ibm_data['Log Returns'].plot(color = 'red')
plt.title('Log Returns for IBM', fontname="arial", fontsize=14, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=12)
plt.ylabel('Log Returns', fontname="arial", fontsize=12)
plt.xticks(fontname="arial", fontsize=10)
plt.yticks(fontname="arial", fontsize=10)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(6, 4)
plt.savefig('4_1_1d.pdf')
jpm_data['Simple Returns'].plot(color = 'blue')
plt.title('Simple Returns for J.P. Morgan', fontname="arial", fontsize=14, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=12)
plt.ylabel('Simple Returns', fontname="arial", fontsize=12)
plt.xticks(fontname="arial", fontsize=10)
plt.yticks(fontname="arial", fontsize=10)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(6, 4)
plt.savefig('4_1_1e.pdf')
jpm_data['Log Returns'].plot(color = 'red')
plt.title('Log Returns for J.P. Morgan', fontname="arial", fontsize=14, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=12)
plt.ylabel('Log Returns', fontname="arial", fontsize=12)
plt.xticks(fontname="arial", fontsize=10)
plt.yticks(fontname="arial", fontsize=10)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(6, 4)
plt.savefig('4_1_1f.pdf')
dji_data['Simple Returns'].plot(color = 'blue')
plt.title('Simple Returns for the Dow Jones Index', fontname="arial", fontsize=14, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=12)
plt.ylabel('Simple Returns', fontname="arial", fontsize=12)
plt.xticks(fontname="arial", fontsize=10)
plt.yticks(fontname="arial", fontsize=10)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(6, 4)
plt.savefig('4_1_1g.pdf')
dji_data['Log Returns'].plot(color = 'red')
plt.title('Log Returns for the Dow Jones Index', fontname="arial", fontsize=14, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=12)
plt.ylabel('Log Returns', fontname="arial", fontsize=12)
plt.xticks(fontname="arial", fontsize=10)
plt.yticks(fontname="arial", fontsize=10)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(6, 4)
plt.savefig('4_1_1h.pdf')
import seaborn as sns
sns.distplot(apple_data['Simple Returns'], hist=True, kde=True, bins=30, vertical = False, color = 'red', hist_kws={'edgecolor':'black'}, kde_kws={'linewidth': 2})
plt.title('Histogram and PDF of Simple Returns of AAPL based on Adj. Close Prices', fontname="arial", fontsize=14, fontweight="bold")
plt.xlabel('Simple Return', fontname="arial", fontsize=14)
plt.ylabel('Density', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=12)
plt.yticks(fontname="arial", fontsize=12)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(7.1, 4.5)
# plt.savefig('4_1_2a.pdf')
/opt/anaconda3/lib/python3.8/site-packages/seaborn/distributions.py:2551: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms). warnings.warn(msg, FutureWarning)
sns.distplot(apple_data['Adj Close'], hist=True, kde=True, bins=40, color = 'blue', hist_kws={'edgecolor':'black'}, kde_kws={'linewidth': 2})
# plt.hist(apple_data['Adj Close'] , bins = 30)
plt.title('Histogram and PDF of Adj. Close Prices of AAPL', fontname="arial", fontsize=14, fontweight="bold")
plt.xlabel('Adj Close Prices', fontname="arial", fontsize=14)
plt.ylabel('Density', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=12)
plt.yticks(fontname="arial", fontsize=12)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(7.1, 4.5)
# plt.savefig('4_1_2b.pdf')
/opt/anaconda3/lib/python3.8/site-packages/seaborn/distributions.py:2551: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms). warnings.warn(msg, FutureWarning)
import seaborn as sns
sns.distplot(ibm_data['Simple Returns'], hist=True, kde=True, bins=40, color = 'red', hist_kws={'edgecolor':'black'}, kde_kws={'linewidth': 2})
plt.title('Histogram and PDF of Simple Returns of IBM based on Adj. Close Prices', fontname="arial", fontsize=14, fontweight="bold")
plt.xlabel('Simple Return', fontname="arial", fontsize=14)
plt.ylabel('Density', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=12)
plt.yticks(fontname="arial", fontsize=12)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(7.1, 4.5)
# plt.savefig('4_1_2c.pdf')
/opt/anaconda3/lib/python3.8/site-packages/seaborn/distributions.py:2551: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms). warnings.warn(msg, FutureWarning)
sns.distplot(ibm_data['Adj Close'], hist=True, kde=True, bins=40, color = 'blue', hist_kws={'edgecolor':'black'}, kde_kws={'linewidth': 2})
plt.title('Histogram and PDF of Adj. Close Prices of IBM', fontname="arial", fontsize=14, fontweight="bold")
plt.xlabel('Adj Close Prices', fontname="arial", fontsize=14)
plt.ylabel('Density', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=12)
plt.yticks(fontname="arial", fontsize=12)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(7.1, 4.5)
plt.savefig('4_1_2d.pdf')
/opt/anaconda3/lib/python3.8/site-packages/seaborn/distributions.py:2551: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms). warnings.warn(msg, FutureWarning)
import seaborn as sns
sns.distplot(jpm_data['Simple Returns'], hist=True, kde=True, bins=40, color = 'red', hist_kws={'edgecolor':'black'}, kde_kws={'linewidth': 2})
plt.title('Histogram and PDF of Simple Returns of JPM based on Adj. Close Prices', fontname="arial", fontsize=14, fontweight="bold")
plt.xlabel('Simple Return', fontname="arial", fontsize=14)
plt.ylabel('Density', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=12)
plt.yticks(fontname="arial", fontsize=12)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(7.1, 4.5)
plt.savefig('4_1_2e.pdf')
/opt/anaconda3/lib/python3.8/site-packages/seaborn/distributions.py:2551: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms). warnings.warn(msg, FutureWarning)
sns.distplot(jpm_data['Adj Close'], hist=True, kde=True, bins=40, color = 'blue', hist_kws={'edgecolor':'black'}, kde_kws={'linewidth': 2})
plt.title('Histogram and PDF of Adj. Close Prices of JPM', fontname="arial", fontsize=14, fontweight="bold")
plt.xlabel('Adj Close Prices', fontname="arial", fontsize=14)
plt.ylabel('Density', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=12)
plt.yticks(fontname="arial", fontsize=12)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(7.1, 4.5)
plt.savefig('4_1_2f.pdf')
/opt/anaconda3/lib/python3.8/site-packages/seaborn/distributions.py:2551: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms). warnings.warn(msg, FutureWarning)
import seaborn as sns
sns.distplot(dji_data['Simple Returns'], hist=True, kde=True, bins=40, color = 'red', hist_kws={'edgecolor':'black'}, kde_kws={'linewidth': 2})
plt.title('Histogram and PDF of Simple Returns of DJI based on Adj. Close Prices', fontname="arial", fontsize=14, fontweight="bold")
plt.xlabel('Simple Return', fontname="arial", fontsize=14)
plt.ylabel('Density', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=12)
plt.yticks(fontname="arial", fontsize=12)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(7.1, 4.5)
plt.savefig('4_1_2g.pdf')
/opt/anaconda3/lib/python3.8/site-packages/seaborn/distributions.py:2551: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms). warnings.warn(msg, FutureWarning)
sns.distplot(dji_data['Adj Close'], hist=True, kde=True, bins=40, color = 'blue', hist_kws={'edgecolor':'black'}, kde_kws={'linewidth': 2})
plt.title('Histogram and PDF of Adj. Close Prices of DJI', fontname="arial", fontsize=14, fontweight="bold")
plt.xlabel('Adj Close Prices', fontname="arial", fontsize=14)
plt.ylabel('Density', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=12)
plt.yticks(fontname="arial", fontsize=12)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(7.1, 4.5)
plt.savefig('4_1_2h.pdf')
/opt/anaconda3/lib/python3.8/site-packages/seaborn/distributions.py:2551: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms). warnings.warn(msg, FutureWarning)
rolling_mean = apple_data['Adj Close'].rolling(5).mean().dropna()
rolling_std = apple_data['Adj Close'].rolling(5).std().dropna()
apple_data['Adj Close'].plot(color = 'red')
rolling_mean.plot(color = 'blue')
(rolling_mean + (1.5*rolling_std)).plot(color = 'green')
(rolling_mean - (1.5*rolling_std)).plot(color = 'orange')
plt.title('Adj. Close Prices, 5-Day Rolling Mean and ±1.5 Std. Relative to Rolling Mean for AAPL', fontname="arial", fontsize=14, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=14)
plt.ylabel('Price', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=14)
plt.yticks(fontname="arial", fontsize=14)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
plt.legend(['Adj Close Price', 'Rolling Mean', 'Upper Bound (Std)', 'Lower Bound (Std)'])
fig = plt.gcf()
fig.set_size_inches(8, 6)
# plt.savefig('4_1_3a.pdf')
rolling_mean = ibm_data['Adj Close'].rolling(5).mean().dropna()
rolling_std = ibm_data['Adj Close'].rolling(5).std().dropna()
ibm_data['Adj Close'].plot(color = 'red')
rolling_mean.plot(color = 'blue')
(rolling_mean + (1.5*rolling_std)).plot(color = 'green')
(rolling_mean - (1.5*rolling_std)).plot(color = 'orange')
plt.title('Adj. Close Prices, 5-Day Rolling Mean and ±1.5 Std. Relative to Rolling Mean for IBM', fontname="arial", fontsize=14, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=14)
plt.ylabel('Price', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=14)
plt.yticks(fontname="arial", fontsize=14)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
plt.legend(['Adj Close Price', 'Rolling Mean', 'Upper Bound (Std)', 'Lower Bound (Std)'])
fig = plt.gcf()
fig.set_size_inches(8, 6)
plt.savefig('4_1_3b.pdf')
rolling_mean = jpm_data['Adj Close'].rolling(5).mean().dropna()
rolling_std = jpm_data['Adj Close'].rolling(5).std().dropna()
jpm_data['Adj Close'].plot(color = 'red')
rolling_mean.plot(color = 'blue')
(rolling_mean + (1.5*rolling_std)).plot(color = 'green')
(rolling_mean - (1.5*rolling_std)).plot(color = 'orange')
plt.title('Adj. Close Prices, 5-Day Rolling Mean and ±1.5 Std. Relative to Rolling Mean for JPM', fontname="arial", fontsize=14, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=14)
plt.ylabel('Price', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=14)
plt.yticks(fontname="arial", fontsize=14)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
plt.legend(['Adj Close Price', 'Rolling Mean', 'Upper Bound (Std)', 'Lower Bound (Std)'])
fig = plt.gcf()
fig.set_size_inches(8, 6)
plt.savefig('4_1_3c.pdf')
rolling_mean = dji_data['Adj Close'].rolling(5).mean().dropna()
rolling_std = dji_data['Adj Close'].rolling(5).std().dropna()
dji_data['Adj Close'].plot(color = 'red')
rolling_mean.plot(color = 'blue')
(rolling_mean + (1.5*rolling_std)).plot(color = 'green')
(rolling_mean - (1.5*rolling_std)).plot(color = 'orange')
plt.title('Adj. Close Prices, 5-Day Rolling Mean and ±1.5 Std. Relative to Rolling Mean for DJI', fontname="arial", fontsize=14, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=14)
plt.ylabel('Price', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=14)
plt.yticks(fontname="arial", fontsize=14)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
plt.legend(['Adj Close Price', 'Rolling Mean', 'Upper Bound (Std)', 'Lower Bound (Std)'])
fig = plt.gcf()
fig.set_size_inches(8, 6)
plt.savefig('4_1_3d.pdf')
adjClose = apple_data['Adj Close'].copy()
rolling_mean = adjClose.rolling(5).mean()
rolling_std = adjClose.rolling(5).std()
x = len(adjClose[adjClose > (rolling_mean + (1.5*rolling_std))]) + len(adjClose[adjClose < (rolling_mean - (1.5*rolling_std))])
print(x)
30
adjClose = ibm_data['Adj Close'].copy()
rolling_mean = adjClose.rolling(5).mean()
rolling_std = adjClose.rolling(5).std()
x = len(adjClose[adjClose > (rolling_mean + (1.5*rolling_std))]) + len(adjClose[adjClose < (rolling_mean - (1.5*rolling_std))])
print(x)
31
adjClose = jpm_data['Adj Close'].copy()
rolling_mean = adjClose.rolling(5).mean()
rolling_std = adjClose.rolling(5).std()
x = len(adjClose[adjClose > (rolling_mean + (1.5*rolling_std))]) + len(adjClose[adjClose < (rolling_mean - (1.5*rolling_std))])
print(x)
33
adjClose = dji_data['Adj Close'].copy()
rolling_mean = adjClose.rolling(5).mean()
rolling_std = adjClose.rolling(5).std()
x = len(adjClose[adjClose > (rolling_mean + (1.5*rolling_std))]) + len(adjClose[adjClose < (rolling_mean - (1.5*rolling_std))])
print(x)
30
rolling_median = apple_data['Adj Close'].rolling(5).median().dropna()
rolling_mad = abs(apple_data['Adj Close'] - apple_data['Adj Close'].rolling(5).median()).median()
apple_data['Adj Close'].plot(color = 'red')
rolling_median.plot(color = 'blue')
(rolling_median + (1.5*rolling_mad)).plot(color = 'green')
(rolling_median - (1.5*rolling_mad)).plot(color = 'orange')
plt.title('Adj. Close Prices, 5-Day Rolling Median and ±1.5 Rolling MAD for AAPL', fontname="arial", fontsize=14, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=14)
plt.ylabel('Price', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=14)
plt.yticks(fontname="arial", fontsize=14)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
plt.legend(['Adj Close Price', 'Rolling Median', 'Upper Bound (MAD)', 'Lower Bound (MAD)'])
fig = plt.gcf()
fig.set_size_inches(8, 6)
plt.savefig('4_1_3e.pdf')
rolling_median = ibm_data['Adj Close'].rolling(5).median().dropna()
rolling_mad = abs(ibm_data['Adj Close'] - ibm_data['Adj Close'].rolling(5).median()).median()
ibm_data['Adj Close'].plot(color = 'red')
rolling_median.plot(color = 'blue')
(rolling_median + (1.5*rolling_mad)).plot(color = 'green')
(rolling_median - (1.5*rolling_mad)).plot(color = 'orange')
plt.title('Adj. Close Prices, 5-Day Rolling Median and ±1.5 Rolling MAD for IBM', fontname="arial", fontsize=14, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=14)
plt.ylabel('Price', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=14)
plt.yticks(fontname="arial", fontsize=14)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
plt.legend(['Adj Close Price', 'Rolling Median', 'Upper Bound (MAD)', 'Lower Bound (MAD)'])
fig = plt.gcf()
fig.set_size_inches(8, 6)
plt.savefig('4_1_3f.pdf')
rolling_median = jpm_data['Adj Close'].rolling(5).median().dropna()
rolling_mad = abs(jpm_data['Adj Close'] - jpm_data['Adj Close'].rolling(5).median()).median()
jpm_data['Adj Close'].plot(color = 'red')
rolling_median.plot(color = 'blue')
(rolling_median + (1.5*rolling_mad)).plot(color = 'green')
(rolling_median - (1.5*rolling_mad)).plot(color = 'orange')
plt.title('Adj. Close Prices, 5-Day Rolling Median and ±1.5 Rolling MAD for JPM', fontname="arial", fontsize=14, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=14)
plt.ylabel('Price', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=14)
plt.yticks(fontname="arial", fontsize=14)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
plt.legend(['Adj Close Price', 'Rolling Median', 'Upper Bound (MAD)', 'Lower Bound (MAD)'])
fig = plt.gcf()
fig.set_size_inches(8, 6)
plt.savefig('4_1_3g.pdf')
rolling_median = dji_data['Adj Close'].rolling(5).median().dropna()
rolling_mad = abs(dji_data['Adj Close'] - dji_data['Adj Close'].rolling(5).median()).median()
dji_data['Adj Close'].plot(color = 'red')
rolling_median.plot(color = 'blue')
(rolling_median + (1.5*rolling_mad)).plot(color = 'green')
(rolling_median - (1.5*rolling_mad)).plot(color = 'orange')
plt.title('Adj. Close Prices, 5-Day Rolling Median and ±1.5 Rolling MAD for DJI', fontname="arial", fontsize=14, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=14)
plt.ylabel('Price', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=14)
plt.yticks(fontname="arial", fontsize=14)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
plt.legend(['Adj Close Price', 'Rolling Median', 'Upper Bound (MAD)', 'Lower Bound (MAD)'])
fig = plt.gcf()
fig.set_size_inches(8, 6)
plt.savefig('4_1_3h.pdf')
adjClose = apple_data['Adj Close'].copy()
rolling_median = adjClose.rolling(5).median()
rolling_mad = abs(adjClose - adjClose.rolling(5).median()).median()
x = len(adjClose[adjClose > (rolling_median + (1.5*rolling_mad))]) + len(adjClose[adjClose < (rolling_median - (1.5*rolling_mad))])
print(x)
90
adjClose = ibm_data['Adj Close'].copy()
rolling_median = adjClose.rolling(5).median()
rolling_mad = abs(adjClose - adjClose.rolling(5).median()).median()
x = len(adjClose[adjClose > (rolling_median + (1.5*rolling_mad))]) + len(adjClose[adjClose < (rolling_median - (1.5*rolling_mad))])
print(x)
89
adjClose = jpm_data['Adj Close'].copy()
rolling_median = adjClose.rolling(5).median()
rolling_mad = abs(adjClose - adjClose.rolling(5).median()).median()
x = len(adjClose[adjClose > (rolling_median + (1.5*rolling_mad))]) + len(adjClose[adjClose < (rolling_median - (1.5*rolling_mad))])
print(x)
92
adjClose = dji_data['Adj Close'].copy()
rolling_median = adjClose.rolling(5).median()
rolling_mad = abs(adjClose - adjClose.rolling(5).median()).median()
x = len(adjClose[adjClose > (rolling_median + (1.5*rolling_mad))]) + len(adjClose[adjClose < (rolling_median - (1.5*rolling_mad))])
print(x)
80
dates = ['2018-05-14', '2018-09-14', '2018-12-14', '2019-01-14']
apple_data_outlier = apple_data.copy()
apple_data_outlier_close = apple_data_outlier['Adj Close']
max_value = apple_data_outlier_close.max()
for date in dates:
apple_data_outlier_close[date] = 1.2 * max_value
rolling_mean = apple_data_outlier_close.rolling(5).mean().dropna()
rolling_std = apple_data_outlier_close.rolling(5).std().dropna()
apple_data_outlier_close.plot(color = 'red')
rolling_mean.plot(color = 'blue')
(rolling_mean + (1.5*rolling_std)).plot(color = 'green')
(rolling_mean - (1.5*rolling_std)).plot(color = 'orange')
plt.title('Adj. Close Prices, 5-Day Rolling Mean and ±1.5 Rolling Std. Dev for AAPL', fontname="arial", fontsize=16, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=16)
plt.ylabel('Price', fontname="arial", fontsize=16)
plt.xticks(fontname="arial", fontsize=16)
plt.yticks(fontname="arial", fontsize=16)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
plt.legend(['Adj Close Price', 'Rolling Mean', 'Upper Bound (Std)', 'Lower Bound (Std)'])
fig = plt.gcf()
fig.set_size_inches(8, 6)
plt.savefig('4_1_4a.pdf')
<ipython-input-39-12e071828b9c>:9: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy apple_data_outlier_close[date] = 1.2 * max_value /opt/anaconda3/lib/python3.8/site-packages/pandas/core/indexing.py:670: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy iloc._setitem_with_indexer(indexer, value)
dates = ['2018-05-14', '2018-09-14', '2018-12-14', '2019-01-14']
apple_data_outlier = apple_data.copy()
apple_data_outlier_close = apple_data_outlier['Adj Close']
max_value = apple_data_outlier_close.max()
for date in dates:
apple_data_outlier_close[date] = 1.2 * max_value
rolling_median = apple_data_outlier_close.rolling(5).median().dropna()
rolling_mad = abs(apple_data_outlier_close - rolling_median).median()
apple_data_outlier_close.plot(color = 'red')
rolling_mean.plot(color = 'blue')
(rolling_median + (1.5*rolling_mad)).plot(color = 'green')
(rolling_median - (1.5*rolling_mad)).plot(color = 'orange')
plt.title('Adj. Close Prices, 5-Day Rolling Median and ±1.5 Rolling MAD for AAPL', fontname="arial", fontsize=16, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=16)
plt.ylabel('Price', fontname="arial", fontsize=16)
plt.xticks(fontname="arial", fontsize=16)
plt.yticks(fontname="arial", fontsize=16)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
plt.legend(['Adj Close Price', 'Rolling Median', 'Upper Bound (MAD)', 'Lower Bound (MAD)'])
fig = plt.gcf()
fig.set_size_inches(8, 6)
plt.savefig('4_1_4b.pdf')
<ipython-input-40-4af550c3ab7e>:9: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy apple_data_outlier_close[date] = 1.2 * max_value /opt/anaconda3/lib/python3.8/site-packages/pandas/core/indexing.py:670: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy iloc._setitem_with_indexer(indexer, value)
dates = ['2018-05-14', '2018-09-14', '2018-12-14', '2019-01-14']
ibm_data_outlier = ibm_data.copy()
ibm_data_outlier_close = ibm_data_outlier['Adj Close']
max_value = ibm_data_outlier_close.max()
for date in dates:
ibm_data_outlier_close[date] = 1.2 * max_value
rolling_mean = ibm_data_outlier_close.rolling(5).mean().dropna()
rolling_std = ibm_data_outlier_close.rolling(5).std().dropna()
ibm_data_outlier_close.plot(color = 'red')
rolling_mean.plot(color = 'blue')
(rolling_mean + (1.5*rolling_std)).plot(color = 'green')
(rolling_mean - (1.5*rolling_std)).plot(color = 'orange')
plt.title('Adj. Close Prices, 5-Day Rolling Mean and ±1.5 Rolling Std. Dev for IBM', fontname="arial", fontsize=16, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=16)
plt.ylabel('Price', fontname="arial", fontsize=16)
plt.xticks(fontname="arial", fontsize=16)
plt.yticks(fontname="arial", fontsize=16)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
plt.legend(['Adj Close Price', 'Rolling Mean', 'Upper Bound (Std)', 'Lower Bound (Std)'])
fig = plt.gcf()
fig.set_size_inches(8, 6)
plt.savefig('4_1_4c.pdf')
<ipython-input-41-10985dbd36cf>:9: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy ibm_data_outlier_close[date] = 1.2 * max_value /opt/anaconda3/lib/python3.8/site-packages/pandas/core/indexing.py:670: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy iloc._setitem_with_indexer(indexer, value)
dates = ['2018-05-14', '2018-09-14', '2018-12-14', '2019-01-14']
ibm_data_outlier = ibm_data.copy()
ibm_data_outlier_close = ibm_data_outlier['Adj Close']
max_value = ibm_data_outlier_close.max()
for date in dates:
ibm_data_outlier_close[date] = 1.2 * max_value
rolling_median = ibm_data_outlier_close.rolling(5).median().dropna()
rolling_mad = abs(ibm_data_outlier_close - rolling_median).median()
ibm_data_outlier_close.plot(color = 'red')
rolling_mean.plot(color = 'blue')
(rolling_median + (1.5*rolling_mad)).plot(color = 'green')
(rolling_median - (1.5*rolling_mad)).plot(color = 'orange')
plt.title('Adj. Close Prices, 5-Day Rolling Median and ±1.5 Rolling MAD for IBM', fontname="arial", fontsize=16, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=16)
plt.ylabel('Price', fontname="arial", fontsize=16)
plt.xticks(fontname="arial", fontsize=16)
plt.yticks(fontname="arial", fontsize=16)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
plt.legend(['Adj Close Price', 'Rolling Median', 'Upper Bound (MAD)', 'Lower Bound (MAD)'])
fig = plt.gcf()
fig.set_size_inches(8, 6)
plt.savefig('4_1_4d.pdf')
<ipython-input-42-b33961ee34b5>:9: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy ibm_data_outlier_close[date] = 1.2 * max_value /opt/anaconda3/lib/python3.8/site-packages/pandas/core/indexing.py:670: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy iloc._setitem_with_indexer(indexer, value)
dates = ['2018-05-14', '2018-09-14', '2018-12-14', '2019-01-14']
jpm_data_outlier = jpm_data.copy()
jpm_data_outlier_close = jpm_data_outlier['Adj Close']
max_value = jpm_data_outlier_close.max()
for date in dates:
jpm_data_outlier_close[date] = 1.2 * max_value
rolling_mean = jpm_data_outlier_close.rolling(5).mean().dropna()
rolling_std = jpm_data_outlier_close.rolling(5).std().dropna()
jpm_data_outlier_close.plot(color = 'red')
rolling_mean.plot(color = 'blue')
(rolling_mean + (1.5*rolling_std)).plot(color = 'green')
(rolling_mean - (1.5*rolling_std)).plot(color = 'orange')
plt.title('Adj. Close Prices, 5-Day Rolling Mean and ±1.5 Rolling Std. Dev for JPM', fontname="arial", fontsize=16, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=16)
plt.ylabel('Price', fontname="arial", fontsize=16)
plt.xticks(fontname="arial", fontsize=16)
plt.yticks(fontname="arial", fontsize=16)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
plt.legend(['Adj Close Price', 'Rolling Mean', 'Upper Bound (Std)', 'Lower Bound (Std)'])
fig = plt.gcf()
fig.set_size_inches(8, 6)
plt.savefig('4_1_4e.pdf')
<ipython-input-43-c4b5eb4edb74>:9: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy jpm_data_outlier_close[date] = 1.2 * max_value /opt/anaconda3/lib/python3.8/site-packages/pandas/core/indexing.py:670: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy iloc._setitem_with_indexer(indexer, value)
dates = ['2018-05-14', '2018-09-14', '2018-12-14', '2019-01-14']
jpm_data_outlier = jpm_data.copy()
jpm_data_outlier_close = jpm_data_outlier['Adj Close']
max_value = jpm_data_outlier_close.max()
for date in dates:
jpm_data_outlier_close[date] = 1.2 * max_value
rolling_median = jpm_data_outlier_close.rolling(5).median().dropna()
rolling_mad = abs(jpm_data_outlier_close - rolling_median).median()
jpm_data_outlier_close.plot(color = 'red')
rolling_mean.plot(color = 'blue')
(rolling_median + (1.5*rolling_mad)).plot(color = 'green')
(rolling_median - (1.5*rolling_mad)).plot(color = 'orange')
plt.title('Adj. Close Prices, 5-Day Rolling Median and ±1.5 Rolling MAD for JPM', fontname="arial", fontsize=16, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=16)
plt.ylabel('Price', fontname="arial", fontsize=16)
plt.xticks(fontname="arial", fontsize=16)
plt.yticks(fontname="arial", fontsize=16)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
plt.legend(['Adj Close Price', 'Rolling Median', 'Upper Bound (MAD)', 'Lower Bound (MAD)'])
fig = plt.gcf()
fig.set_size_inches(8, 6)
plt.savefig('4_1_4f.pdf')
<ipython-input-44-13b987dd9821>:9: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy jpm_data_outlier_close[date] = 1.2 * max_value /opt/anaconda3/lib/python3.8/site-packages/pandas/core/indexing.py:670: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy iloc._setitem_with_indexer(indexer, value)
dates = ['2018-05-14', '2018-09-14', '2018-12-14', '2019-01-14']
dji_data_outlier = dji_data.copy()
dji_data_outlier_close = dji_data_outlier['Adj Close']
max_value = dji_data_outlier_close.max()
for date in dates:
dji_data_outlier_close[date] = 1.2 * max_value
rolling_mean = dji_data_outlier_close.rolling(5).mean().dropna()
rolling_std = dji_data_outlier_close.rolling(5).std().dropna()
dji_data_outlier_close.plot(color = 'red')
rolling_mean.plot(color = 'blue')
(rolling_mean + (1.5*rolling_std)).plot(color = 'green')
(rolling_mean - (1.5*rolling_std)).plot(color = 'orange')
plt.title('Adj. Close Prices, 5-Day Rolling Mean and ±1.5 Rolling Std. Dev for DJI', fontname="arial", fontsize=16, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=16)
plt.ylabel('Price', fontname="arial", fontsize=16)
plt.xticks(fontname="arial", fontsize=16)
plt.yticks(fontname="arial", fontsize=16)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
plt.legend(['Adj Close Price', 'Rolling Mean', 'Upper Bound (Std)', 'Lower Bound (Std)'])
fig = plt.gcf()
fig.set_size_inches(8, 6)
plt.savefig('4_1_4g.pdf')
<ipython-input-45-4fba357efef7>:9: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy dji_data_outlier_close[date] = 1.2 * max_value /opt/anaconda3/lib/python3.8/site-packages/pandas/core/indexing.py:670: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy iloc._setitem_with_indexer(indexer, value)
dates = ['2018-05-14', '2018-09-14', '2018-12-14', '2019-01-14']
dji_data_outlier = dji_data.copy()
dji_data_outlier_close = dji_data_outlier['Adj Close']
max_value = dji_data_outlier_close.max()
for date in dates:
dji_data_outlier_close[date] = 1.2 * max_value
rolling_median = dji_data_outlier_close.rolling(5).median().dropna()
rolling_mad = abs(dji_data_outlier_close - rolling_median).median()
dji_data_outlier_close.plot(color = 'red')
rolling_mean.plot(color = 'blue')
(rolling_median + (1.5*rolling_mad)).plot(color = 'green')
(rolling_median - (1.5*rolling_mad)).plot(color = 'orange')
plt.title('Adj. Close Prices, 5-Day Rolling Median and ±1.5 Rolling MAD for DJI', fontname="arial", fontsize=16, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=16)
plt.ylabel('Price', fontname="arial", fontsize=16)
plt.xticks(fontname="arial", fontsize=16)
plt.yticks(fontname="arial", fontsize=16)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
plt.legend(['Adj Close Price', 'Rolling Median', 'Upper Bound (MAD)', 'Lower Bound (MAD)'])
fig = plt.gcf()
fig.set_size_inches(8, 6)
plt.savefig('4_1_4h.pdf')
<ipython-input-46-750adb28d049>:9: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy dji_data_outlier_close[date] = 1.2 * max_value /opt/anaconda3/lib/python3.8/site-packages/pandas/core/indexing.py:670: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy iloc._setitem_with_indexer(indexer, value)
import matplotlib.pyplot as plt
adjClose = apple_data['Adj Close'].copy()
plt.boxplot(adjClose)
plt.title('Box Plot for Apple', fontname="arial", fontsize=16, fontweight="bold")
plt.ylabel('Price', fontname="arial", fontsize=16)
plt.xticks(fontname="arial", fontsize=16)
plt.yticks(fontname="arial", fontsize=16)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
fig = plt.gcf()
fig.set_size_inches(6, 4)
plt.savefig('4_1_5a.pdf')
adjClose = ibm_data['Adj Close'].copy()
plt.boxplot(adjClose)
plt.title('Box Plot for IBM', fontname="arial", fontsize=16, fontweight="bold")
plt.ylabel('Price', fontname="arial", fontsize=16)
plt.xticks(fontname="arial", fontsize=16)
plt.yticks(fontname="arial", fontsize=16)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
fig = plt.gcf()
fig.set_size_inches(6, 4)
plt.savefig('4_1_5b.pdf')
adjClose = jpm_data['Adj Close'].copy()
plt.boxplot(adjClose)
plt.title('Box Plot for J.P. Morgan', fontname="arial", fontsize=16, fontweight="bold")
plt.ylabel('Price', fontname="arial", fontsize=16)
plt.xticks(fontname="arial", fontsize=16)
plt.yticks(fontname="arial", fontsize=16)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
fig = plt.gcf()
fig.set_size_inches(6, 4)
plt.savefig('4_1_5c.pdf')
adjClose = dji_data['Adj Close'].copy()
plt.boxplot(adjClose)
plt.title('Box Plot for the Dow Jones Index', fontname="arial", fontsize=16, fontweight="bold")
plt.ylabel('Price', fontname="arial", fontsize=16)
plt.xticks(fontname="arial", fontsize=16)
plt.yticks(fontname="arial", fontsize=16)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
fig = plt.gcf()
fig.set_size_inches(6, 4)
plt.savefig('4_1_5d.pdf')
def med(df):
df_sorted = df.sort_values()
middle_index = round(len(df_sorted) / 2)
return df_sorted[middle_index]
def interquartile_range(df):
df_sorted = df.sort_values()
quarter_index = round(len(df_sorted) / 4)
three_quarter_index = round(3 * len(df_sorted) / 4)
Q25 = df_sorted[quarter_index]
Q75 = df_sorted[three_quarter_index]
return Q75 - Q25
def median_absolute_deviation(df):
initial_median = med(df)
absolute_deviations = abs(df - initial_median)
return med(absolute_deviations)
from sklearn import linear_model
ret_stock = apple_data[['Simple Returns']].dropna()
ret_dji = dji_data[['Simple Returns']].dropna()
regress = linear_model.LinearRegression()
X = ret_dji.copy().values.reshape(-1, 1)
y = ret_stock.copy().values.reshape(-1, 1)
regress.fit(X, y)
predict = regress.predict(X)
intercept = float(regress.intercept_)
coefficient = float(regress.coef_)
plt.plot(ret_stock.index, y, color = 'blue')
plt.plot(ret_stock.index, predict, color = 'orange')
plt.legend(['Simple Returns','Predicted Returns'])
plt.title('Predicted Returns of Apple using OLS', fontname="arial", fontsize=16, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=14)
plt.ylabel('Simple Returns', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=14)
plt.yticks(fontname="arial", fontsize=14)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(8, 6)
print("Intercept:", intercept)
print("Coefficient:", coefficient)
# plt.savefig('4_3_1a.pdf')
print("R-Squared Score: ", regress.score(X, y))
Intercept: 0.00016466014892917348 Coefficient: 1.3255798434655275 R-Squared Score: 0.5165176143267884
plt.scatter(X, y, color = 'blue')
plt.plot(X, predict, color = 'red')
plt.legend(['OLS Prediction', 'Actual Scatter'])
plt.title('OLS Regression of Apple Simple Returns vs DJI Simple Returns', fontname="arial", fontsize=16, fontweight="bold")
plt.xlabel('DJI Daily Simple Returns', fontname="arial", fontsize=14)
plt.ylabel('Apple Daily Simple Returns', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=14)
plt.yticks(fontname="arial", fontsize=14)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(8, 6)
plt.savefig('4_3_1b.pdf')
from sklearn import linear_model
ret_stock = ibm_data[['Simple Returns']].dropna()
ret_dji = dji_data[['Simple Returns']].dropna()
regress = linear_model.LinearRegression()
X = ret_dji.copy().values.reshape(-1, 1)
y = ret_stock.copy().values.reshape(-1, 1)
regress.fit(X, y)
predict = regress.predict(X)
intercept = float(regress.intercept_)
coefficient = float(regress.coef_)
plt.plot(ret_stock.index, y, color = 'blue')
plt.plot(ret_stock.index, predict, color = 'orange')
plt.legend(['Simple Returns','Predicted Returns'])
plt.title('Predicted Returns of IBM using OLS', fontname="arial", fontsize=16, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=14)
plt.ylabel('Simple Returns', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=14)
plt.yticks(fontname="arial", fontsize=14)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(8, 6)
print("Intercept:", intercept)
print("Coefficient:", coefficient)
# plt.savefig('4_3_1c.pdf')
print("R-Squared Score: ", regress.score(X, y))
Intercept: -0.000440572495482164 Coefficient: 0.9600924554800796 R-Squared Score: 0.4177725981268693
plt.scatter(X, y, color = 'blue')
plt.plot(X, predict, color = 'red')
plt.legend(['OLS Prediction', 'Actual Scatter'])
plt.title('OLS Regression of IBM Simple Returns vs DJI Simple Returns', fontname="arial", fontsize=16, fontweight="bold")
plt.xlabel('DJI Daily Simple Returns', fontname="arial", fontsize=14)
plt.ylabel('IBM Daily Simple Returns', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=14)
plt.yticks(fontname="arial", fontsize=14)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(8, 6)
plt.savefig('4_3_1d.pdf')
from sklearn import linear_model
ret_stock = jpm_data[['Simple Returns']].dropna()
ret_dji = dji_data[['Simple Returns']].dropna()
regress = linear_model.LinearRegression()
X = ret_dji.copy().values.reshape(-1, 1)
y = ret_stock.copy().values.reshape(-1, 1)
regress.fit(X, y)
predict = regress.predict(X)
intercept = float(regress.intercept_)
coefficient = float(regress.coef_)
plt.plot(ret_stock.index, y, color = 'blue')
plt.plot(ret_stock.index, predict, color = 'orange')
plt.legend(['Simple Returns','Predicted Returns'])
plt.title('Predicted Returns of J.P. Morgan using OLS', fontname="arial", fontsize=16, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=14)
plt.ylabel('Simple Returns', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=14)
plt.yticks(fontname="arial", fontsize=14)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(8, 6)
print("Intercept:", intercept)
print("Coefficient:", coefficient)
# plt.savefig('4_3_1e.pdf')
print("R-Squared Score: ", regress.score(X, y))
Intercept: -0.00031633075996005287 Coefficient: 0.9314081808843367 R-Squared Score: 0.5558638461848688
plt.scatter(X, y, color = 'blue')
plt.plot(X, predict, color = 'red')
plt.legend(['OLS Prediction', 'Actual Scatter'])
plt.title('OLS Regression of J.P. Morgan Simple Returns vs DJI Simple Returns', fontname="arial", fontsize=16, fontweight="bold")
plt.xlabel('DJI Daily Simple Returns', fontname="arial", fontsize=14)
plt.ylabel('J.P. Morgan Daily Simple Returns', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=14)
plt.yticks(fontname="arial", fontsize=14)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(8, 6)
plt.savefig('4_3_1f.pdf')
from sklearn import linear_model
ret_stock = apple_data[['Simple Returns']].dropna()
ret_dji = dji_data[['Simple Returns']].dropna()
regress = linear_model.HuberRegressor()
X = ret_dji.copy().values.reshape(-1, 1)
y = ret_stock.copy().values.reshape(-1, 1)
regress.fit(X, y)
predict = regress.predict(X)
intercept = float(regress.intercept_)
coefficient = float(regress.coef_)
plt.plot(ret_stock.index, y, color = 'blue')
plt.plot(ret_stock.index, predict, color = 'orange')
plt.legend(['Simple Returns','Predicted Returns'])
plt.title('Predicted Returns of Apple using Huber Regression', fontname="arial", fontsize=16, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=14)
plt.ylabel('Simple Returns', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=14)
plt.yticks(fontname="arial", fontsize=14)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(8, 6)
print("Intercept:", intercept)
print("Coefficient:", coefficient)
# plt.savefig('4_3_2a.pdf')
print("R-Squared Score: ", regress.score(X, y))
/opt/anaconda3/lib/python3.8/site-packages/sklearn/utils/validation.py:72: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel(). return f(**kwargs)
Intercept: -0.00013037065614373493 Coefficient: 1.2702124335516514 R-Squared Score: 0.5153648281193706
plt.scatter(X, y, color = 'blue')
plt.plot(X, predict, color = 'red')
plt.legend(['Huber Regression Prediction', 'Actual Scatter'])
plt.title('Huber Regression of Apple Simple Returns vs DJI Simple Returns', fontname="arial", fontsize=16, fontweight="bold")
plt.xlabel('DJI Daily Simple Returns', fontname="arial", fontsize=14)
plt.ylabel('Apple Daily Simple Returns', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=14)
plt.yticks(fontname="arial", fontsize=14)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(8, 6)
plt.savefig('4_3_2b.pdf')
from sklearn import linear_model
ret_stock = ibm_data[['Simple Returns']].dropna()
ret_dji = dji_data[['Simple Returns']].dropna()
regress = linear_model.HuberRegressor()
X = ret_dji.copy().values.reshape(-1, 1)
y = ret_stock.copy().values.reshape(-1, 1)
regress.fit(X, y)
predict = regress.predict(X)
intercept = float(regress.intercept_)
coefficient = float(regress.coef_)
plt.plot(ret_stock.index, y, color = 'blue')
plt.plot(ret_stock.index, predict, color = 'orange')
plt.legend(['Simple Returns','Predicted Returns'])
plt.title('Predicted Returns of IBM using Huber Regression', fontname="arial", fontsize=16, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=14)
plt.ylabel('Simple Returns', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=14)
plt.yticks(fontname="arial", fontsize=14)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(8, 6)
print("Intercept:", intercept)
print("Coefficient:", coefficient)
# plt.savefig('4_3_2c.pdf')
print("R-Squared Score: ", regress.score(X, y))
/opt/anaconda3/lib/python3.8/site-packages/sklearn/utils/validation.py:72: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel(). return f(**kwargs)
Intercept: -0.0005094349614468659 Coefficient: 0.9735620712623559 R-Squared Score: 0.4176721932886538
plt.scatter(X, y, color = 'blue')
plt.plot(X, predict, color = 'red')
plt.legend(['Huber Regression Prediction', 'Actual Scatter'])
plt.title('Huber Regression of IBM Simple Returns vs DJI Simple Returns', fontname="arial", fontsize=16, fontweight="bold")
plt.xlabel('DJI Daily Simple Returns', fontname="arial", fontsize=14)
plt.ylabel('IBM Daily Simple Returns', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=14)
plt.yticks(fontname="arial", fontsize=14)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(8, 6)
plt.savefig('4_3_2d.pdf')
from sklearn import linear_model
ret_stock = jpm_data[['Simple Returns']].dropna()
ret_dji = dji_data[['Simple Returns']].dropna()
regress = linear_model.HuberRegressor()
X = ret_dji.copy().values.reshape(-1, 1)
y = ret_stock.copy().values.reshape(-1, 1)
regress.fit(X, y)
predict = regress.predict(X)
intercept = float(regress.intercept_)
coefficient = float(regress.coef_)
plt.plot(ret_stock.index, y, color = 'blue')
plt.plot(ret_stock.index, predict, color = 'orange')
plt.legend(['Simple Returns','Predicted Returns'])
plt.title('Predicted Returns of J.P. Morgan using Huber Regression', fontname="arial", fontsize=16, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=14)
plt.ylabel('Simple Returns', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=14)
plt.yticks(fontname="arial", fontsize=14)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(8, 6)
print("Intercept:", intercept)
print("Coefficient:", coefficient)
# plt.savefig('4_3_2e.pdf')
print("R-Squared Score: ", regress.score(X, y))
/opt/anaconda3/lib/python3.8/site-packages/sklearn/utils/validation.py:72: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel(). return f(**kwargs)
Intercept: -0.0008009614209572162 Coefficient: 0.9196620617499993 R-Squared Score: 0.5543856144824422
plt.scatter(X, y, color = 'blue')
plt.plot(X, predict, color = 'red')
plt.legend(['Huber Regression Prediction', 'Actual Scatter'])
plt.title('Huber Regression of J.P. Morgan Simple Returns vs DJI Simple Returns', fontname="arial", fontsize=16, fontweight="bold")
plt.xlabel('DJI Daily Simple Returns', fontname="arial", fontsize=14)
plt.ylabel('J.P. Morgan Daily Simple Returns', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=14)
plt.yticks(fontname="arial", fontsize=14)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(8, 6)
plt.savefig('4_3_2f.pdf')
from sklearn import linear_model
import numpy as np
import pandas as pd
from statsmodels import robust
from scipy.stats import iqr
from scipy import stats
import matplotlib.pyplot as plt
AAPL = pd.read_csv('AAPL.csv', index_col=0)
IBM = pd.read_csv('IBM.csv', index_col=0)
JPM = pd.read_csv('JPM.csv', index_col=0)
DJI = pd.read_csv('DJI.csv', index_col=0)
AAPL.index = pd.to_datetime(AAPL.index)
IBM.index = pd.to_datetime(IBM.index)
DJI.index = pd.to_datetime(DJI.index)
JPM.index = pd.to_datetime(JPM.index)
apple_data = AAPL.copy()
ibm_data = IBM.copy()
dji_data = DJI.copy()
jpm_data = JPM.copy()
dates = ['2018-05-14', '2018-09-14', '2018-12-14', '2019-01-14']
stock_data_outlier = apple_data.copy()
dji_data_outlier = dji_data.copy()
stock_data_outlier['Simple Returns'] = stock_data_outlier['Adj Close'].pct_change()
dji_data_outlier['Simple Returns'] = dji_data_outlier['Adj Close'].pct_change()
stock_max_value = stock_data_outlier['Adj Close'].max()
dji_max_value = dji_data_outlier['Adj Close'].max()
for date in dates:
stock_data_outlier['Adj Close'][date] = 1.2 * stock_max_value
stock_data_outlier['Simple Returns Modified'] = stock_data_outlier['Adj Close'].dropna().pct_change()
dji_data_outlier['Simple Returns Modified'] = dji_data_outlier['Adj Close'].dropna().pct_change()
ret_stock = stock_data_outlier[['Simple Returns Modified']].dropna()
ret_dji = dji_data_outlier[['Simple Returns Modified']].dropna()
regress = linear_model.LinearRegression()
X = ret_dji.copy().values.reshape(-1, 1)
y = ret_stock.copy().values.reshape(-1, 1)
regress.fit(X, y)
predict = regress.predict(X)
intercept = float(regress.intercept_)
coefficient = float(regress.coef_)
print("Intercept: ", intercept)
print("Coefficient:", coefficient)
print("R-Squared Score: ", regress.score(X, y))
plt.scatter(X, y, color = 'blue')
plt.plot(X, predict, color = 'red')
plt.legend(['OLS Prediction', 'Actual Scatter'])
plt.title('OLS Regression of Apple Simple Returns vs DJI Simple Returns with Outliers', fontname="arial", fontsize=14, fontweight="bold")
plt.xlabel('DJI Daily Simple Returns', fontname="arial", fontsize=14)
plt.ylabel('Apple Daily Simple Returns', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=14)
plt.yticks(fontname="arial", fontsize=14)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
plt.ylim([-0.1, 0.08])
fig = plt.gcf()
fig.set_size_inches(8, 6)
plt.savefig('4_3_3a.pdf')
<ipython-input-64-5eef0dc9c733>:38: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy stock_data_outlier['Adj Close'][date] = 1.2 * stock_max_value /opt/anaconda3/lib/python3.8/site-packages/pandas/core/indexing.py:670: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy iloc._setitem_with_indexer(indexer, value)
Intercept: 0.003549490722840998 Coefficient: 1.0898121696643095 R-Squared Score: 0.016532159461199125
from sklearn import linear_model
dates = ['2018-05-14', '2018-09-14', '2018-12-14', '2019-01-14']
stock_data_outlier = ibm_data.copy()
dji_data_outlier = dji_data.copy()
stock_data_outlier['Simple Returns'] = stock_data_outlier['Adj Close'].pct_change()
dji_data_outlier['Simple Returns'] = dji_data_outlier['Adj Close'].pct_change()
stock_max_value = stock_data_outlier['Adj Close'].max()
dji_max_value = dji_data_outlier['Adj Close'].max()
for date in dates:
stock_data_outlier['Adj Close'][date] = 1.2 * stock_max_value
stock_data_outlier['Simple Returns Modified'] = stock_data_outlier['Adj Close'].dropna().pct_change()
dji_data_outlier['Simple Returns Modified'] = dji_data_outlier['Adj Close'].dropna().pct_change()
ret_stock = stock_data_outlier[['Simple Returns Modified']].dropna()
ret_dji = dji_data_outlier[['Simple Returns Modified']].dropna()
regress = linear_model.LinearRegression()
X = ret_dji.copy().values.reshape(-1, 1)
y = ret_stock.copy().values.reshape(-1, 1)
regress.fit(X, y)
predict = regress.predict(X)
intercept = float(regress.intercept_)
coefficient = float(regress.coef_)
print("Intercept: ", intercept)
print("Coefficient:", coefficient)
print("R-Squared Score: ", regress.score(X, y))
plt.scatter(X, y, color = 'blue')
plt.plot(X, predict, color = 'red')
plt.legend(['OLS Prediction', 'Actual Scatter'])
plt.title('OLS Regression of IBM Simple Returns vs DJI Simple Returns with Outliers', fontname="arial", fontsize=14, fontweight="bold")
plt.xlabel('DJI Daily Simple Returns', fontname="arial", fontsize=14)
plt.ylabel('IBM Daily Simple Returns', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=14)
plt.yticks(fontname="arial", fontsize=14)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
plt.ylim([-0.08, 0.08])
fig = plt.gcf()
fig.set_size_inches(8, 6)
plt.savefig('4_3_3b.pdf')
<ipython-input-65-fcc475885032>:14: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy stock_data_outlier['Adj Close'][date] = 1.2 * stock_max_value /opt/anaconda3/lib/python3.8/site-packages/pandas/core/indexing.py:670: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy iloc._setitem_with_indexer(indexer, value)
Intercept: 0.0017162600387344896 Coefficient: 0.7904432787562923 R-Squared Score: 0.014326326309465842
from sklearn import linear_model
dates = ['2018-05-14', '2018-09-14', '2018-12-14', '2019-01-14']
stock_data_outlier = jpm_data.copy()
dji_data_outlier = dji_data.copy()
stock_data_outlier['Simple Returns'] = stock_data_outlier['Adj Close'].pct_change()
dji_data_outlier['Simple Returns'] = dji_data_outlier['Adj Close'].pct_change()
stock_max_value = stock_data_outlier['Adj Close'].max()
dji_max_value = dji_data_outlier['Adj Close'].max()
for date in dates:
stock_data_outlier['Adj Close'][date] = 1.2 * stock_max_value
stock_data_outlier['Simple Returns Modified'] = stock_data_outlier['Adj Close'].dropna().pct_change()
dji_data_outlier['Simple Returns Modified'] = dji_data_outlier['Adj Close'].dropna().pct_change()
ret_stock = stock_data_outlier[['Simple Returns Modified']].dropna()
ret_dji = dji_data_outlier[['Simple Returns Modified']].dropna()
regress = linear_model.LinearRegression()
X = ret_dji.copy().values.reshape(-1, 1)
y = ret_stock.copy().values.reshape(-1, 1)
regress.fit(X, y)
predict = regress.predict(X)
intercept = float(regress.intercept_)
coefficient = float(regress.coef_)
print("Intercept: ", intercept)
print("Coefficient:", coefficient)
print("R-Squared Score: ", regress.score(X, y))
plt.scatter(X, y, color = 'blue')
plt.plot(X, predict, color = 'red')
plt.legend(['OLS Prediction', 'Actual Scatter'])
plt.title('OLS Regression of JPM Simple Returns vs DJI Simple Returns with Outliers', fontname="arial", fontsize=14, fontweight="bold")
plt.xlabel('DJI Daily Simple Returns', fontname="arial", fontsize=14)
plt.ylabel('J.P. Morgan Daily Simple Returns', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=14)
plt.yticks(fontname="arial", fontsize=14)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
plt.ylim([-0.04, 0.04])
fig = plt.gcf()
fig.set_size_inches(8, 6)
plt.savefig('4_3_3c.pdf')
<ipython-input-66-e60ec39fbbbc>:14: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy stock_data_outlier['Adj Close'][date] = 1.2 * stock_max_value /opt/anaconda3/lib/python3.8/site-packages/pandas/core/indexing.py:670: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy iloc._setitem_with_indexer(indexer, value)
Intercept: 0.0010421467748870134 Coefficient: 0.8435894617700498 R-Squared Score: 0.026162346446048867
from sklearn import linear_model
dates = ['2018-05-14', '2018-09-14', '2018-12-14', '2019-01-14']
stock_data_outlier = apple_data.copy()
dji_data_outlier = dji_data.copy()
stock_data_outlier['Simple Returns'] = stock_data_outlier['Adj Close'].pct_change()
dji_data_outlier['Simple Returns'] = dji_data_outlier['Adj Close'].pct_change()
stock_max_value = stock_data_outlier['Adj Close'].max()
dji_max_value = dji_data_outlier['Adj Close'].max()
for date in dates:
stock_data_outlier['Adj Close'][date] = 1.2 * stock_max_value
stock_data_outlier['Simple Returns Modified'] = stock_data_outlier['Adj Close'].dropna().pct_change()
dji_data_outlier['Simple Returns Modified'] = dji_data_outlier['Adj Close'].dropna().pct_change()
ret_stock = stock_data_outlier[['Simple Returns Modified']].dropna()
ret_dji = dji_data_outlier[['Simple Returns Modified']].dropna()
regress = linear_model.HuberRegressor()
X = ret_dji.copy().values.reshape(-1, 1)
y = ret_stock.copy().values.reshape(-1, 1)
regress.fit(X, y)
predict = regress.predict(X)
intercept = float(regress.intercept_)
coefficient = float(regress.coef_)
print("Intercept: ", intercept)
print("Coefficient:", coefficient)
print("R-Squared Score: ", regress.score(X, y))
plt.scatter(X, y, color = 'blue')
plt.plot(X, predict, color = 'red')
plt.legend(['Huber Prediction', 'Actual Scatter'])
plt.title('Huber Regression of Apple Simple Returns vs DJI Simple Returns with Outliers', fontname="arial", fontsize=14, fontweight="bold")
plt.xlabel('DJI Daily Simple Returns', fontname="arial", fontsize=14)
plt.ylabel('Apple Daily Simple Returns', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=14)
plt.yticks(fontname="arial", fontsize=14)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
plt.ylim([-0.1, 0.075])
fig = plt.gcf()
fig.set_size_inches(8, 6)
plt.savefig('4_3_3d.pdf')
<ipython-input-67-aae10690ba0a>:14: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy stock_data_outlier['Adj Close'][date] = 1.2 * stock_max_value /opt/anaconda3/lib/python3.8/site-packages/pandas/core/indexing.py:670: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy iloc._setitem_with_indexer(indexer, value) /opt/anaconda3/lib/python3.8/site-packages/sklearn/utils/validation.py:72: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel(). return f(**kwargs)
Intercept: 7.5229485921091805e-06 Coefficient: 1.269470572076923 R-Squared Score: 0.014517140673291018
from sklearn import linear_model
dates = ['2018-05-14', '2018-09-14', '2018-12-14', '2019-01-14']
stock_data_outlier = ibm_data.copy()
dji_data_outlier = dji_data.copy()
stock_data_outlier['Simple Returns'] = stock_data_outlier['Adj Close'].pct_change()
dji_data_outlier['Simple Returns'] = dji_data_outlier['Adj Close'].pct_change()
stock_max_value = stock_data_outlier['Adj Close'].max()
dji_max_value = dji_data_outlier['Adj Close'].max()
for date in dates:
stock_data_outlier['Adj Close'][date] = 1.2 * stock_max_value
stock_data_outlier['Simple Returns Modified'] = stock_data_outlier['Adj Close'].dropna().pct_change()
dji_data_outlier['Simple Returns Modified'] = dji_data_outlier['Adj Close'].dropna().pct_change()
ret_stock = stock_data_outlier[['Simple Returns Modified']].dropna()
ret_dji = dji_data_outlier[['Simple Returns Modified']].dropna()
regress = linear_model.HuberRegressor()
X = ret_dji.copy().values.reshape(-1, 1)
y = ret_stock.copy().values.reshape(-1, 1)
regress.fit(X, y)
predict = regress.predict(X)
intercept = float(regress.intercept_)
coefficient = float(regress.coef_)
print("Intercept: ", intercept)
print("Coefficient:", coefficient)
print("R-Squared Score: ", regress.score(X, y))
plt.scatter(X, y, color = 'blue')
plt.plot(X, predict, color = 'red')
plt.legend(['Huber Prediction', 'Actual Scatter'])
plt.title('Huber Regression of IBM Simple Returns vs DJI Simple Returns with Outliers', fontname="arial", fontsize=14, fontweight="bold")
plt.ylim([-0.08, 0.08])
plt.xlabel('DJI Daily Simple Returns', fontname="arial", fontsize=14)
plt.ylabel('IBM Daily Simple Returns', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=14)
plt.yticks(fontname="arial", fontsize=14)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(8, 6)
plt.savefig('4_3_3e.pdf')
<ipython-input-68-72f301905917>:14: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy stock_data_outlier['Adj Close'][date] = 1.2 * stock_max_value /opt/anaconda3/lib/python3.8/site-packages/pandas/core/indexing.py:670: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy iloc._setitem_with_indexer(indexer, value) /opt/anaconda3/lib/python3.8/site-packages/sklearn/utils/validation.py:72: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel(). return f(**kwargs)
Intercept: -0.0005072169944084335 Coefficient: 0.9738519062813633 R-Squared Score: 0.01255141281946115
from sklearn import linear_model
dates = ['2018-05-14', '2018-06-17', '2018-09-14', '2018-12-14', '2019-01-14', '2019-02-14', '2018-06-13', '2018-06-20']
stock_data_outlier = jpm_data.copy()
dji_data_outlier = dji_data.copy()
stock_data_outlier['Simple Returns'] = stock_data_outlier['Adj Close'].pct_change()
dji_data_outlier['Simple Returns'] = dji_data_outlier['Adj Close'].pct_change()
stock_max_value = stock_data_outlier['Adj Close'].max()
dji_max_value = dji_data_outlier['Adj Close'].max()
for date in dates:
stock_data_outlier['Adj Close'][date] = 1.2 * stock_max_value
stock_data_outlier['Simple Returns Modified'] = stock_data_outlier['Adj Close'].dropna().pct_change()
dji_data_outlier['Simple Returns Modified'] = dji_data_outlier['Adj Close'].dropna().pct_change()
ret_stock = stock_data_outlier[['Simple Returns Modified']].dropna()
ret_dji = dji_data_outlier[['Simple Returns Modified']].dropna()
regress = linear_model.HuberRegressor()
X = ret_dji.copy().values.reshape(-1, 1)
y = ret_stock.copy().values.reshape(-1, 1)
regress.fit(X, y)
predict = regress.predict(X)
intercept = float(regress.intercept_)
coefficient = float(regress.coef_)
print("Intercept: ", intercept)
print("Coefficient:", coefficient)
print("R-Squared Score: ", regress.score(X, y))
plt.scatter(X, y, color = 'blue')
plt.plot(X, predict, color = 'red')
plt.legend(['Huber Prediction', 'Actual Scatter'])
plt.ylim([-0.04, 0.04])
plt.xlim([-0.03, 0.05])
plt.title('Huber Regression of JPM Simple Returns vs DJI Simple Returns with Outliers', fontname="arial", fontsize=14, fontweight="bold")
plt.xlabel('DJI Daily Simple Returns', fontname="arial", fontsize=14)
plt.ylabel('J.P. Morgan Daily Simple Returns', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=14)
plt.yticks(fontname="arial", fontsize=14)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(8, 6)
plt.savefig('4_3_3f.pdf')
<ipython-input-69-a70e96b68951>:14: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy stock_data_outlier['Adj Close'][date] = 1.2 * stock_max_value /opt/anaconda3/lib/python3.8/site-packages/pandas/core/indexing.py:670: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy iloc._setitem_with_indexer(indexer, value) /opt/anaconda3/lib/python3.8/site-packages/sklearn/utils/validation.py:72: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel(). return f(**kwargs)
Intercept: -0.0010399307087863468 Coefficient: 0.9320167989688328 R-Squared Score: 0.004331192488909341
apple_data = pd.read_csv('AAPL.csv', index_col=0)
apple_data.index = pd.to_datetime(apple_data.index)
adjClose = apple_data['Adj Close'].dropna()
apple_data['Ret'] = apple_data['Adj Close'].pct_change()
MA20 = adjClose.rolling(20).mean()
MA50 = adjClose.rolling(50).mean()
adjClose.plot(color = 'blue')
MA20.plot(color = 'green')
MA50.plot(color = 'red')
plt.fill_between(adjClose.index, MA20, MA50, where = MA20 > MA50, color = 'g', alpha = 0.3)
plt.fill_between(adjClose.index, MA20, MA50, where = MA20 < MA50, color = 'r', alpha = 0.3)
plt.legend(['Adjusted Close Prices', 'MA20', 'MA50', 'Buy', 'Sell'])
plt.title('Moving Average Crossover Strategy for Apple', fontname="arial", fontsize=14, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=14)
plt.ylabel('Price', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=14)
plt.yticks(fontname="arial", fontsize=14)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(8, 6)
plt.savefig('4_4_1a.pdf')
apple_data['Ret'] = apple_data['Adj Close'].pct_change()
apple_data['Action'] = np.ones(len(apple_data)) * np.nan
apple_data['20 MA'] = apple_data['Adj Close'].rolling(20).mean().dropna()
apple_data['50 MA'] = apple_data['Adj Close'].rolling(50).mean().dropna()
apple_data.loc[apple_data['20 MA'] >= apple_data['50 MA'], 'Action'] = 1
apple_data.loc[apple_data['20 MA'] < apple_data['50 MA'], 'Action'] = -1
apple_data['cumulative'] = np.cumsum(apple_data['Ret'] * apple_data['Action'])
apple_data['cumulative'].plot(color = 'blue')
plt.title('Cumulative Returns of Apple using Moving Average Crossover Strategy', fontname="arial", fontsize=14, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=14)
plt.ylabel('Cumulative Returns', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=14)
plt.yticks(fontname="arial", fontsize=14)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(8, 6)
plt.savefig('4_4_1b.pdf')
apple_data = pd.read_csv('AAPL.csv', index_col=0)
apple_data.index = pd.to_datetime(apple_data.index)
adjClose = apple_data['Adj Close'].dropna()
# dates = ['2018-05-14', '2018-09-14', '2018-12-14', '2019-01-14', '2018-06-21', '2018-10-10', '2018-07-23']
dates = ['2018-05-14', '2018-09-14', '2018-12-14', '2019-01-14']
adjClose_outlier = adjClose.copy()
max_value = adjClose_outlier.max()
for date in dates:
adjClose_outlier[date] = max_value * 1.2
MA20 = adjClose_outlier.rolling(20).mean()
MA50 = adjClose_outlier.rolling(50).mean()
adjClose_outlier.plot(color = 'blue')
MA20.plot(color = 'green')
MA50.plot(color = 'red')
plt.fill_between(adjClose_outlier.index, MA20, MA50, where = MA20 > MA50, color = 'g', alpha = 0.3)
plt.fill_between(adjClose_outlier.index, MA20, MA50, where = MA20 < MA50, color = 'r', alpha = 0.3)
plt.legend(['Adjusted Close Prices', 'MA20', 'MA50', 'Buy', 'Sell'])
plt.title('Moving Average Crossover Strategy for Apple with Outliers', fontname="arial", fontsize=16, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=14)
plt.ylabel('Price', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=14)
plt.yticks(fontname="arial", fontsize=14)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(8, 6)
plt.savefig('4_4_1c.pdf')
# dates = ['2018-05-14', '2018-09-14', '2018-12-14', '2019-01-14', '2018-06-21', '2018-10-10', '2018-07-23']
dates = ['2018-05-14', '2018-09-14', '2018-12-14', '2019-01-14']
max_value = apple_data['Adj Close'].max()
for date in dates:
apple_data['Adj Close'][date] = max_value * 1.2
apple_data['Ret'] = apple_data['Adj Close'].pct_change()
apple_data['Action'] = np.ones(len(apple_data)) * np.nan
apple_data['20 MA'] = apple_data['Adj Close'].rolling(20).mean().dropna()
apple_data['50 MA'] = apple_data['Adj Close'].rolling(50).mean().dropna()
apple_data.loc[apple_data['20 MA'] >= apple_data['50 MA'], 'Action'] = 1
apple_data.loc[apple_data['20 MA'] < apple_data['50 MA'], 'Action'] = -1
apple_data['cumulative'] = np.cumsum(apple_data['Ret'] * apple_data['Action'])
apple_data['cumulative'].plot(color = 'blue')
plt.title('Cumulative Return of Apple with Outliers using Moving Average Crossover Strategy', fontname="arial", fontsize=14, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=14)
plt.ylabel('Cumulative Returns', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=14)
plt.yticks(fontname="arial", fontsize=14)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(8, 6)
plt.savefig('4_4_1d.pdf')
<ipython-input-73-170362f3c8d8>:7: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy apple_data['Adj Close'][date] = max_value * 1.2 /opt/anaconda3/lib/python3.8/site-packages/pandas/core/indexing.py:670: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy iloc._setitem_with_indexer(indexer, value)
apple_data = pd.read_csv('IBM.csv', index_col=0)
apple_data.index = pd.to_datetime(apple_data.index)
adjClose = apple_data['Adj Close'].dropna()
apple_data['Ret'] = apple_data['Adj Close'].pct_change()
MA20 = adjClose.rolling(20).mean()
MA50 = adjClose.rolling(50).mean()
adjClose.plot(color = 'blue')
MA20.plot(color = 'green')
MA50.plot(color = 'red')
plt.fill_between(adjClose.index, MA20, MA50, where = MA20 > MA50, color = 'g', alpha = 0.3)
plt.fill_between(adjClose.index, MA20, MA50, where = MA20 < MA50, color = 'r', alpha = 0.3)
plt.legend(['Adjusted Close Prices', 'MA20', 'MA50', 'Buy', 'Sell'])
plt.title('Moving Average Crossover Strategy for IBM', fontname="arial", fontsize=14, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=14)
plt.ylabel('Price', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=14)
plt.yticks(fontname="arial", fontsize=14)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(8, 6)
plt.savefig('4_4_1e.pdf')
apple_data['Ret'] = apple_data['Adj Close'].pct_change()
apple_data['Action'] = np.ones(len(apple_data)) * np.nan
apple_data['20 MA'] = apple_data['Adj Close'].rolling(20).mean().dropna()
apple_data['50 MA'] = apple_data['Adj Close'].rolling(50).mean().dropna()
apple_data.loc[apple_data['20 MA'] >= apple_data['50 MA'], 'Action'] = 1
apple_data.loc[apple_data['20 MA'] < apple_data['50 MA'], 'Action'] = -1
apple_data['cumulative'] = np.cumsum(apple_data['Ret'] * apple_data['Action'])
apple_data['cumulative'].plot(color = 'blue')
plt.title('Cumulative Returns of IBM using Moving Average Crossover Strategy', fontname="arial", fontsize=14, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=14)
plt.ylabel('Cumulative Returns', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=14)
plt.yticks(fontname="arial", fontsize=14)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(8, 6)
plt.savefig('4_4_1f.pdf')
apple_data = pd.read_csv('IBM.csv', index_col=0)
apple_data.index = pd.to_datetime(apple_data.index)
adjClose = apple_data['Adj Close'].dropna()
# dates = ['2018-05-14', '2018-09-14', '2018-12-14', '2019-01-14', '2018-06-21', '2018-10-10', '2018-07-23']
dates = ['2018-05-14', '2018-09-14', '2018-12-14', '2019-01-14']
adjClose_outlier = adjClose.copy()
max_value = adjClose_outlier.max()
for date in dates:
adjClose_outlier[date] = max_value * 1.2
MA20 = adjClose_outlier.rolling(20).mean()
MA50 = adjClose_outlier.rolling(50).mean()
adjClose_outlier.plot(color = 'blue')
MA20.plot(color = 'green')
MA50.plot(color = 'red')
plt.fill_between(adjClose_outlier.index, MA20, MA50, where = MA20 > MA50, color = 'g', alpha = 0.3)
plt.fill_between(adjClose_outlier.index, MA20, MA50, where = MA20 < MA50, color = 'r', alpha = 0.3)
plt.legend(['Adjusted Close Prices', 'MA20', 'MA50', 'Buy', 'Sell'])
plt.title('Moving Average Crossover Strategy for IBM with Outliers', fontname="arial", fontsize=16, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=14)
plt.ylabel('Price', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=14)
plt.yticks(fontname="arial", fontsize=14)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(8, 6)
plt.savefig('4_4_1g.pdf')
# dates = ['2018-05-14', '2018-09-14', '2018-12-14', '2019-01-14', '2018-06-21', '2018-10-10', '2018-07-23']
dates = ['2018-05-14', '2018-09-14', '2018-12-14', '2019-01-14']
max_value = apple_data['Adj Close'].max()
for date in dates:
apple_data['Adj Close'][date] = max_value * 1.2
apple_data['Ret'] = apple_data['Adj Close'].pct_change()
apple_data['Action'] = np.ones(len(apple_data)) * np.nan
apple_data['20 MA'] = apple_data['Adj Close'].rolling(20).mean().dropna()
apple_data['50 MA'] = apple_data['Adj Close'].rolling(50).mean().dropna()
apple_data.loc[apple_data['20 MA'] >= apple_data['50 MA'], 'Action'] = 1
apple_data.loc[apple_data['20 MA'] < apple_data['50 MA'], 'Action'] = -1
apple_data['cumulative'] = np.cumsum(apple_data['Ret'] * apple_data['Action'])
apple_data['cumulative'].plot(color = 'blue')
plt.title('Cumulative Return of IBM with Outliers using Moving Average Crossover Strategy', fontname="arial", fontsize=14, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=14)
plt.ylabel('Cumulative Returns', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=14)
plt.yticks(fontname="arial", fontsize=14)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(8, 6)
plt.savefig('4_4_1h.pdf')
<ipython-input-77-44246cbe3ef3>:7: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy apple_data['Adj Close'][date] = max_value * 1.2 /opt/anaconda3/lib/python3.8/site-packages/pandas/core/indexing.py:670: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy iloc._setitem_with_indexer(indexer, value)
apple_data = pd.read_csv('DJI.csv', index_col=0)
apple_data.index = pd.to_datetime(apple_data.index)
adjClose = apple_data['Adj Close'].dropna()
apple_data['Ret'] = apple_data['Adj Close'].pct_change()
MA20 = adjClose.rolling(20).mean()
MA50 = adjClose.rolling(50).mean()
adjClose.plot(color = 'blue')
MA20.plot(color = 'green')
MA50.plot(color = 'red')
plt.fill_between(adjClose.index, MA20, MA50, where = MA20 > MA50, color = 'g', alpha = 0.3)
plt.fill_between(adjClose.index, MA20, MA50, where = MA20 < MA50, color = 'r', alpha = 0.3)
plt.legend(['Adjusted Close Prices', 'MA20', 'MA50', 'Buy', 'Sell'])
plt.title('Moving Average Crossover Strategy for DJI', fontname="arial", fontsize=14, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=14)
plt.ylabel('Price', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=14)
plt.yticks(fontname="arial", fontsize=14)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(8, 6)
plt.savefig('4_4_1i.pdf')
apple_data['Ret'] = apple_data['Adj Close'].pct_change()
apple_data['Action'] = np.ones(len(apple_data)) * np.nan
apple_data['20 MA'] = apple_data['Adj Close'].rolling(20).mean().dropna()
apple_data['50 MA'] = apple_data['Adj Close'].rolling(50).mean().dropna()
apple_data.loc[apple_data['20 MA'] >= apple_data['50 MA'], 'Action'] = 1
apple_data.loc[apple_data['20 MA'] < apple_data['50 MA'], 'Action'] = -1
apple_data['cumulative'] = np.cumsum(apple_data['Ret'] * apple_data['Action'])
apple_data['cumulative'].plot(color = 'blue')
plt.title('Cumulative Returns of DJI using Moving Average Crossover Strategy', fontname="arial", fontsize=14, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=14)
plt.ylabel('Cumulative Returns', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=14)
plt.yticks(fontname="arial", fontsize=14)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(8, 6)
plt.savefig('4_4_1j.pdf')
apple_data = pd.read_csv('DJI.csv', index_col=0)
apple_data.index = pd.to_datetime(apple_data.index)
adjClose = apple_data['Adj Close'].dropna()
# dates = ['2018-05-14', '2018-09-14', '2018-12-14', '2019-01-14', '2018-06-21', '2018-10-10', '2018-07-23']
dates = ['2018-05-14', '2018-09-14', '2018-12-14', '2019-01-14']
adjClose_outlier = adjClose.copy()
max_value = adjClose_outlier.max()
for date in dates:
adjClose_outlier[date] = max_value * 1.2
MA20 = adjClose_outlier.rolling(20).mean()
MA50 = adjClose_outlier.rolling(50).mean()
adjClose_outlier.plot(color = 'blue')
MA20.plot(color = 'green')
MA50.plot(color = 'red')
plt.fill_between(adjClose_outlier.index, MA20, MA50, where = MA20 > MA50, color = 'g', alpha = 0.3)
plt.fill_between(adjClose_outlier.index, MA20, MA50, where = MA20 < MA50, color = 'r', alpha = 0.3)
plt.legend(['Adjusted Close Prices', 'MA20', 'MA50', 'Buy', 'Sell'])
plt.title('Moving Average Crossover Strategy for DJI with Outliers', fontname="arial", fontsize=16, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=14)
plt.ylabel('Price', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=14)
plt.yticks(fontname="arial", fontsize=14)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(8, 6)
plt.savefig('4_4_1k.pdf')
# dates = ['2018-05-14', '2018-09-14', '2018-12-14', '2019-01-14', '2018-06-21', '2018-10-10', '2018-07-23']
dates = ['2018-05-14', '2018-09-14', '2018-12-14', '2019-01-14']
max_value = apple_data['Adj Close'].max()
for date in dates:
apple_data['Adj Close'][date] = max_value * 1.2
apple_data['Ret'] = apple_data['Adj Close'].pct_change()
apple_data['Action'] = np.ones(len(apple_data)) * np.nan
apple_data['20 MA'] = apple_data['Adj Close'].rolling(20).mean().dropna()
apple_data['50 MA'] = apple_data['Adj Close'].rolling(50).mean().dropna()
apple_data.loc[apple_data['20 MA'] >= apple_data['50 MA'], 'Action'] = 1
apple_data.loc[apple_data['20 MA'] < apple_data['50 MA'], 'Action'] = -1
apple_data['cumulative'] = np.cumsum(apple_data['Ret'] * apple_data['Action'])
apple_data['cumulative'].plot(color = 'blue')
plt.title('Cumulative Return of DJI with Outliers using Moving Average Crossover Strategy', fontname="arial", fontsize=14, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=14)
plt.ylabel('Cumulative Returns', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=14)
plt.yticks(fontname="arial", fontsize=14)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(8, 6)
plt.savefig('4_4_1l.pdf')
<ipython-input-81-98effbfc3d96>:7: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy apple_data['Adj Close'][date] = max_value * 1.2 /opt/anaconda3/lib/python3.8/site-packages/pandas/core/indexing.py:670: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy iloc._setitem_with_indexer(indexer, value)
apple_data = pd.read_csv('JPM.csv', index_col=0)
apple_data.index = pd.to_datetime(apple_data.index)
adjClose = apple_data['Adj Close'].dropna()
apple_data['Ret'] = apple_data['Adj Close'].pct_change()
MA20 = adjClose.rolling(20).mean()
MA50 = adjClose.rolling(50).mean()
adjClose.plot(color = 'blue')
MA20.plot(color = 'green')
MA50.plot(color = 'red')
plt.fill_between(adjClose.index, MA20, MA50, where = MA20 > MA50, color = 'g', alpha = 0.3)
plt.fill_between(adjClose.index, MA20, MA50, where = MA20 < MA50, color = 'r', alpha = 0.3)
plt.legend(['Adjusted Close Prices', 'MA20', 'MA50', 'Buy', 'Sell'])
plt.title('Moving Average Crossover Strategy for JPM', fontname="arial", fontsize=14, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=14)
plt.ylabel('Price', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=14)
plt.yticks(fontname="arial", fontsize=14)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(8, 6)
plt.savefig('4_4_1m.pdf')
apple_data['Ret'] = apple_data['Adj Close'].pct_change()
apple_data['Action'] = np.ones(len(apple_data)) * np.nan
apple_data['20 MA'] = apple_data['Adj Close'].rolling(20).mean().dropna()
apple_data['50 MA'] = apple_data['Adj Close'].rolling(50).mean().dropna()
apple_data.loc[apple_data['20 MA'] >= apple_data['50 MA'], 'Action'] = 1
apple_data.loc[apple_data['20 MA'] < apple_data['50 MA'], 'Action'] = -1
apple_data['cumulative'] = np.cumsum(apple_data['Ret'] * apple_data['Action'])
apple_data['cumulative'].plot(color = 'blue')
plt.title('Cumulative Returns of JPM using Moving Average Crossover Strategy', fontname="arial", fontsize=14, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=14)
plt.ylabel('Cumulative Returns', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=14)
plt.yticks(fontname="arial", fontsize=14)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(8, 6)
plt.savefig('4_4_1n.pdf')
apple_data = pd.read_csv('JPM.csv', index_col=0)
apple_data.index = pd.to_datetime(apple_data.index)
adjClose = apple_data['Adj Close'].dropna()
# dates = ['2018-05-14', '2018-09-14', '2018-12-14', '2019-01-14', '2018-06-21', '2018-10-10', '2018-07-23']
dates = ['2018-05-14', '2018-09-14', '2018-12-14', '2019-01-14']
adjClose_outlier = adjClose.copy()
max_value = adjClose_outlier.max()
for date in dates:
adjClose_outlier[date] = max_value * 1.2
MA20 = adjClose_outlier.rolling(20).mean()
MA50 = adjClose_outlier.rolling(50).mean()
adjClose_outlier.plot(color = 'blue')
MA20.plot(color = 'green')
MA50.plot(color = 'red')
plt.fill_between(adjClose_outlier.index, MA20, MA50, where = MA20 > MA50, color = 'g', alpha = 0.3)
plt.fill_between(adjClose_outlier.index, MA20, MA50, where = MA20 < MA50, color = 'r', alpha = 0.3)
plt.legend(['Adjusted Close Prices', 'MA20', 'MA50', 'Buy', 'Sell'])
plt.title('Moving Average Crossover Strategy for JPM with Outliers', fontname="arial", fontsize=16, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=14)
plt.ylabel('Price', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=14)
plt.yticks(fontname="arial", fontsize=14)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(8, 6)
plt.savefig('4_4_1o.pdf')
# dates = ['2018-05-14', '2018-09-14', '2018-12-14', '2019-01-14', '2018-06-21', '2018-10-10', '2018-07-23']
dates = ['2018-05-14', '2018-09-14', '2018-12-14', '2019-01-14']
max_value = apple_data['Adj Close'].max()
for date in dates:
apple_data['Adj Close'][date] = max_value * 1.2
apple_data['Ret'] = apple_data['Adj Close'].pct_change()
apple_data['Action'] = np.ones(len(apple_data)) * np.nan
apple_data['20 MA'] = apple_data['Adj Close'].rolling(20).mean().dropna()
apple_data['50 MA'] = apple_data['Adj Close'].rolling(50).mean().dropna()
apple_data.loc[apple_data['20 MA'] >= apple_data['50 MA'], 'Action'] = 1
apple_data.loc[apple_data['20 MA'] < apple_data['50 MA'], 'Action'] = -1
apple_data['cumulative'] = np.cumsum(apple_data['Ret'] * apple_data['Action'])
apple_data['cumulative'].plot(color = 'blue')
plt.title('Cumulative Return of JPM with Outliers using Moving Average Crossover Strategy', fontname="arial", fontsize=14, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=14)
plt.ylabel('Cumulative Returns', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=14)
plt.yticks(fontname="arial", fontsize=14)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(8, 6)
plt.savefig('4_4_1p.pdf')
<ipython-input-85-b5ffebefc0bf>:7: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy apple_data['Adj Close'][date] = max_value * 1.2 /opt/anaconda3/lib/python3.8/site-packages/pandas/core/indexing.py:670: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy iloc._setitem_with_indexer(indexer, value)
apple_data = pd.read_csv('AAPL.csv', index_col=0)
apple_data.index = pd.to_datetime(apple_data.index)
adjClose = apple_data['Adj Close'].dropna()
apple_data['Ret'] = apple_data['Adj Close'].pct_change()
MA20 = adjClose.rolling(20).median()
MA50 = adjClose.rolling(50).median()
adjClose.plot(color = 'blue')
MA20.plot(color = 'green')
MA50.plot(color = 'red')
plt.fill_between(adjClose.index, MA20, MA50, where = MA20 > MA50, color = 'g', alpha = 0.3)
plt.fill_between(adjClose.index, MA20, MA50, where = MA20 < MA50, color = 'r', alpha = 0.3)
plt.legend(['Adjusted Close Prices', 'ME20', 'ME50', 'Buy', 'Sell'])
plt.title('Moving Median Crossover Strategy for Apple', fontname="arial", fontsize=14, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=14)
plt.ylabel('Price', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=14)
plt.yticks(fontname="arial", fontsize=14)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(8, 6)
plt.savefig('4_4_2a.pdf')
apple_data['Ret'] = apple_data['Adj Close'].pct_change()
apple_data['Action'] = np.ones(len(apple_data)) * np.nan
apple_data['20 MA'] = apple_data['Adj Close'].rolling(20).median().dropna()
apple_data['50 MA'] = apple_data['Adj Close'].rolling(50).median().dropna()
apple_data.loc[apple_data['20 MA'] >= apple_data['50 MA'], 'Action'] = 1
apple_data.loc[apple_data['20 MA'] < apple_data['50 MA'], 'Action'] = -1
apple_data['cumulative'] = np.cumsum(apple_data['Ret'] * apple_data['Action'])
apple_data['cumulative'].plot(color = 'blue')
plt.title('Cumulative Returns of Apple using Moving Median Crossover Strategy', fontname="arial", fontsize=14, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=14)
plt.ylabel('Cumulative Returns', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=14)
plt.yticks(fontname="arial", fontsize=14)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(8, 6)
plt.savefig('4_4_2b.pdf')
apple_data = pd.read_csv('AAPL.csv', index_col=0)
apple_data.index = pd.to_datetime(apple_data.index)
adjClose = apple_data['Adj Close'].dropna()
# dates = ['2018-05-14', '2018-09-14', '2018-12-14', '2019-01-14', '2018-06-21', '2018-10-10', '2018-07-23']
dates = ['2018-05-14', '2018-09-14', '2018-12-14', '2019-01-14']
adjClose_outlier = adjClose.copy()
max_value = adjClose_outlier.max()
for date in dates:
adjClose_outlier[date] = max_value * 1.2
MA20 = adjClose_outlier.rolling(20).median()
MA50 = adjClose_outlier.rolling(50).median()
adjClose_outlier.plot(color = 'blue')
MA20.plot(color = 'green')
MA50.plot(color = 'red')
plt.fill_between(adjClose_outlier.index, MA20, MA50, where = MA20 > MA50, color = 'g', alpha = 0.3)
plt.fill_between(adjClose_outlier.index, MA20, MA50, where = MA20 < MA50, color = 'r', alpha = 0.3)
plt.legend(['Adjusted Close Prices', 'ME20', 'ME50', 'Buy', 'Sell'])
plt.title('Moving Median Crossover Strategy for Apple with Outliers', fontname="arial", fontsize=16, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=14)
plt.ylabel('Price', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=14)
plt.yticks(fontname="arial", fontsize=14)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(8, 6)
plt.savefig('4_4_2c.pdf')
# dates = ['2018-05-14', '2018-09-14', '2018-12-14', '2019-01-14', '2018-06-21', '2018-10-10', '2018-07-23']
dates = ['2018-05-14', '2018-09-14', '2018-12-14', '2019-01-14']
max_value = apple_data['Adj Close'].max()
for date in dates:
apple_data['Adj Close'][date] = max_value * 1.2
apple_data['Ret'] = apple_data['Adj Close'].pct_change()
apple_data['Action'] = np.ones(len(apple_data)) * np.nan
apple_data['20 MA'] = apple_data['Adj Close'].rolling(20).median().dropna()
apple_data['50 MA'] = apple_data['Adj Close'].rolling(50).median().dropna()
apple_data.loc[apple_data['20 MA'] >= apple_data['50 MA'], 'Action'] = 1
apple_data.loc[apple_data['20 MA'] < apple_data['50 MA'], 'Action'] = -1
apple_data['cumulative'] = np.cumsum(apple_data['Ret'] * apple_data['Action'])
apple_data['cumulative'].plot(color = 'blue')
plt.title('Cumulative Return of Apple with Outliers using Moving Median Crossover Strategy', fontname="arial", fontsize=14, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=14)
plt.ylabel('Cumulative Returns', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=14)
plt.yticks(fontname="arial", fontsize=14)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(8, 6)
plt.savefig('4_4_2d.pdf')
<ipython-input-89-c4f9ede07771>:7: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy apple_data['Adj Close'][date] = max_value * 1.2 /opt/anaconda3/lib/python3.8/site-packages/pandas/core/indexing.py:670: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy iloc._setitem_with_indexer(indexer, value)
apple_data = pd.read_csv('IBM.csv', index_col=0)
apple_data.index = pd.to_datetime(apple_data.index)
adjClose = apple_data['Adj Close'].dropna()
apple_data['Ret'] = apple_data['Adj Close'].pct_change()
MA20 = adjClose.rolling(20).median()
MA50 = adjClose.rolling(50).median()
adjClose.plot(color = 'blue')
MA20.plot(color = 'green')
MA50.plot(color = 'red')
plt.fill_between(adjClose.index, MA20, MA50, where = MA20 > MA50, color = 'g', alpha = 0.3)
plt.fill_between(adjClose.index, MA20, MA50, where = MA20 < MA50, color = 'r', alpha = 0.3)
plt.legend(['Adjusted Close Prices', 'ME20', 'ME50', 'Buy', 'Sell'])
plt.title('Moving Median Crossover Strategy for IBM', fontname="arial", fontsize=14, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=14)
plt.ylabel('Price', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=14)
plt.yticks(fontname="arial", fontsize=14)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(8, 6)
plt.savefig('4_4_2e.pdf')
apple_data['Ret'] = apple_data['Adj Close'].pct_change()
apple_data['Action'] = np.ones(len(apple_data)) * np.nan
apple_data['20 MA'] = apple_data['Adj Close'].rolling(20).median().dropna()
apple_data['50 MA'] = apple_data['Adj Close'].rolling(50).median().dropna()
apple_data.loc[apple_data['20 MA'] >= apple_data['50 MA'], 'Action'] = 1
apple_data.loc[apple_data['20 MA'] < apple_data['50 MA'], 'Action'] = -1
apple_data['cumulative'] = np.cumsum(apple_data['Ret'] * apple_data['Action'])
apple_data['cumulative'].plot(color = 'blue')
plt.title('Cumulative Returns of IBM using Moving Median Crossover Strategy', fontname="arial", fontsize=14, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=14)
plt.ylabel('Cumulative Returns', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=14)
plt.yticks(fontname="arial", fontsize=14)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(8, 6)
plt.savefig('4_4_2f.pdf')
apple_data = pd.read_csv('IBM.csv', index_col=0)
apple_data.index = pd.to_datetime(apple_data.index)
adjClose = apple_data['Adj Close'].dropna()
# dates = ['2018-05-14', '2018-09-14', '2018-12-14', '2019-01-14', '2018-06-21', '2018-10-10', '2018-07-23']
dates = ['2018-05-14', '2018-09-14', '2018-12-14', '2019-01-14']
adjClose_outlier = adjClose.copy()
max_value = adjClose_outlier.max()
for date in dates:
adjClose_outlier[date] = max_value * 1.2
MA20 = adjClose_outlier.rolling(20).median()
MA50 = adjClose_outlier.rolling(50).median()
adjClose_outlier.plot(color = 'blue')
MA20.plot(color = 'green')
MA50.plot(color = 'red')
plt.fill_between(adjClose_outlier.index, MA20, MA50, where = MA20 > MA50, color = 'g', alpha = 0.3)
plt.fill_between(adjClose_outlier.index, MA20, MA50, where = MA20 < MA50, color = 'r', alpha = 0.3)
plt.legend(['Adjusted Close Prices', 'ME20', 'ME50', 'Buy', 'Sell'])
plt.title('Moving Median Crossover Strategy for IBM with Outliers', fontname="arial", fontsize=16, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=14)
plt.ylabel('Price', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=14)
plt.yticks(fontname="arial", fontsize=14)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(8, 6)
plt.savefig('4_4_2g.pdf')
# dates = ['2018-05-14', '2018-09-14', '2018-12-14', '2019-01-14', '2018-06-21', '2018-10-10', '2018-07-23']
dates = ['2018-05-14', '2018-09-14', '2018-12-14', '2019-01-14']
max_value = apple_data['Adj Close'].max()
for date in dates:
apple_data['Adj Close'][date] = max_value * 1.2
apple_data['Ret'] = apple_data['Adj Close'].pct_change()
apple_data['Action'] = np.ones(len(apple_data)) * np.nan
apple_data['20 MA'] = apple_data['Adj Close'].rolling(20).median().dropna()
apple_data['50 MA'] = apple_data['Adj Close'].rolling(50).median().dropna()
apple_data.loc[apple_data['20 MA'] >= apple_data['50 MA'], 'Action'] = 1
apple_data.loc[apple_data['20 MA'] < apple_data['50 MA'], 'Action'] = -1
apple_data['cumulative'] = np.cumsum(apple_data['Ret'] * apple_data['Action'])
apple_data['cumulative'].plot(color = 'blue')
plt.title('Cumulative Return of IBM with Outliers using Moving Median Crossover Strategy', fontname="arial", fontsize=14, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=14)
plt.ylabel('Cumulative Returns', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=14)
plt.yticks(fontname="arial", fontsize=14)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(8, 6)
plt.savefig('4_4_2h.pdf')
<ipython-input-93-c4ac5e891a5c>:7: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy apple_data['Adj Close'][date] = max_value * 1.2 /opt/anaconda3/lib/python3.8/site-packages/pandas/core/indexing.py:670: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy iloc._setitem_with_indexer(indexer, value)
apple_data = pd.read_csv('JPM.csv', index_col=0)
apple_data.index = pd.to_datetime(apple_data.index)
adjClose = apple_data['Adj Close'].dropna()
apple_data['Ret'] = apple_data['Adj Close'].pct_change()
MA20 = adjClose.rolling(20).median()
MA50 = adjClose.rolling(50).median()
adjClose.plot(color = 'blue')
MA20.plot(color = 'green')
MA50.plot(color = 'red')
plt.fill_between(adjClose.index, MA20, MA50, where = MA20 > MA50, color = 'g', alpha = 0.3)
plt.fill_between(adjClose.index, MA20, MA50, where = MA20 < MA50, color = 'r', alpha = 0.3)
plt.legend(['Adjusted Close Prices', 'ME20', 'ME50', 'Buy', 'Sell'])
plt.title('Moving Median Crossover Strategy for JPM', fontname="arial", fontsize=14, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=14)
plt.ylabel('Price', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=14)
plt.yticks(fontname="arial", fontsize=14)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(8, 6)
plt.savefig('4_4_2i.pdf')
apple_data['Ret'] = apple_data['Adj Close'].pct_change()
apple_data['Action'] = np.ones(len(apple_data)) * np.nan
apple_data['20 MA'] = apple_data['Adj Close'].rolling(20).median().dropna()
apple_data['50 MA'] = apple_data['Adj Close'].rolling(50).median().dropna()
apple_data.loc[apple_data['20 MA'] >= apple_data['50 MA'], 'Action'] = 1
apple_data.loc[apple_data['20 MA'] < apple_data['50 MA'], 'Action'] = -1
apple_data['cumulative'] = np.cumsum(apple_data['Ret'] * apple_data['Action'])
apple_data['cumulative'].plot(color = 'blue')
plt.title('Cumulative Returns of JPM using Moving Median Crossover Strategy', fontname="arial", fontsize=14, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=14)
plt.ylabel('Cumulative Returns', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=14)
plt.yticks(fontname="arial", fontsize=14)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(8, 6)
plt.savefig('4_4_2j.pdf')
apple_data = pd.read_csv('JPM.csv', index_col=0)
apple_data.index = pd.to_datetime(apple_data.index)
adjClose = apple_data['Adj Close'].dropna()
# dates = ['2018-05-14', '2018-09-14', '2018-12-14', '2019-01-14', '2018-06-21', '2018-10-10', '2018-07-23']
dates = ['2018-05-14', '2018-09-14', '2018-12-14', '2019-01-14']
adjClose_outlier = adjClose.copy()
max_value = adjClose_outlier.max()
for date in dates:
adjClose_outlier[date] = max_value * 1.2
MA20 = adjClose_outlier.rolling(20).median()
MA50 = adjClose_outlier.rolling(50).median()
adjClose_outlier.plot(color = 'blue')
MA20.plot(color = 'green')
MA50.plot(color = 'red')
plt.fill_between(adjClose_outlier.index, MA20, MA50, where = MA20 > MA50, color = 'g', alpha = 0.3)
plt.fill_between(adjClose_outlier.index, MA20, MA50, where = MA20 < MA50, color = 'r', alpha = 0.3)
plt.legend(['Adjusted Close Prices', 'ME20', 'ME50', 'Buy', 'Sell'])
plt.title('Moving Median Crossover Strategy for JPM with Outliers', fontname="arial", fontsize=16, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=14)
plt.ylabel('Price', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=14)
plt.yticks(fontname="arial", fontsize=14)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(8, 6)
plt.savefig('4_4_2k.pdf')
# dates = ['2018-05-14', '2018-09-14', '2018-12-14', '2019-01-14', '2018-06-21', '2018-10-10', '2018-07-23']
dates = ['2018-05-14', '2018-09-14', '2018-12-14', '2019-01-14']
max_value = apple_data['Adj Close'].max()
for date in dates:
apple_data['Adj Close'][date] = max_value * 1.2
apple_data['Ret'] = apple_data['Adj Close'].pct_change()
apple_data['Action'] = np.ones(len(apple_data)) * np.nan
apple_data['20 MA'] = apple_data['Adj Close'].rolling(20).median().dropna()
apple_data['50 MA'] = apple_data['Adj Close'].rolling(50).median().dropna()
apple_data.loc[apple_data['20 MA'] >= apple_data['50 MA'], 'Action'] = 1
apple_data.loc[apple_data['20 MA'] < apple_data['50 MA'], 'Action'] = -1
apple_data['cumulative'] = np.cumsum(apple_data['Ret'] * apple_data['Action'])
apple_data['cumulative'].plot(color = 'blue')
plt.title('Cumulative Return of JPM with Outliers using Moving Median Crossover Strategy', fontname="arial", fontsize=14, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=14)
plt.ylabel('Cumulative Returns', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=14)
plt.yticks(fontname="arial", fontsize=14)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(8, 6)
plt.savefig('4_4_2l.pdf')
<ipython-input-97-87992cc63321>:7: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy apple_data['Adj Close'][date] = max_value * 1.2 /opt/anaconda3/lib/python3.8/site-packages/pandas/core/indexing.py:670: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy iloc._setitem_with_indexer(indexer, value)
apple_data = pd.read_csv('DJI.csv', index_col=0)
apple_data.index = pd.to_datetime(apple_data.index)
adjClose = apple_data['Adj Close'].dropna()
apple_data['Ret'] = apple_data['Adj Close'].pct_change()
MA20 = adjClose.rolling(20).median()
MA50 = adjClose.rolling(50).median()
adjClose.plot(color = 'blue')
MA20.plot(color = 'green')
MA50.plot(color = 'red')
plt.fill_between(adjClose.index, MA20, MA50, where = MA20 > MA50, color = 'g', alpha = 0.3)
plt.fill_between(adjClose.index, MA20, MA50, where = MA20 < MA50, color = 'r', alpha = 0.3)
plt.legend(['Adjusted Close Prices', 'ME20', 'ME50', 'Buy', 'Sell'])
plt.title('Moving Median Crossover Strategy for DJI', fontname="arial", fontsize=14, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=14)
plt.ylabel('Price', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=14)
plt.yticks(fontname="arial", fontsize=14)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(8, 6)
plt.savefig('4_4_2m.pdf')
apple_data['Ret'] = apple_data['Adj Close'].pct_change()
apple_data['Action'] = np.ones(len(apple_data)) * np.nan
apple_data['20 MA'] = apple_data['Adj Close'].rolling(20).median().dropna()
apple_data['50 MA'] = apple_data['Adj Close'].rolling(50).median().dropna()
apple_data.loc[apple_data['20 MA'] >= apple_data['50 MA'], 'Action'] = 1
apple_data.loc[apple_data['20 MA'] < apple_data['50 MA'], 'Action'] = -1
apple_data['cumulative'] = np.cumsum(apple_data['Ret'] * apple_data['Action'])
apple_data['cumulative'].plot(color = 'blue')
plt.title('Cumulative Returns of DJI using Moving Median Crossover Strategy', fontname="arial", fontsize=14, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=14)
plt.ylabel('Cumulative Returns', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=14)
plt.yticks(fontname="arial", fontsize=14)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(8, 6)
plt.savefig('4_4_2n.pdf')
apple_data = pd.read_csv('DJI.csv', index_col=0)
apple_data.index = pd.to_datetime(apple_data.index)
adjClose = apple_data['Adj Close'].dropna()
# dates = ['2018-05-14', '2018-09-14', '2018-12-14', '2019-01-14', '2018-06-21', '2018-10-10', '2018-07-23']
dates = ['2018-05-14', '2018-09-14', '2018-12-14', '2019-01-14']
adjClose_outlier = adjClose.copy()
max_value = adjClose_outlier.max()
for date in dates:
adjClose_outlier[date] = max_value * 1.2
MA20 = adjClose_outlier.rolling(20).median()
MA50 = adjClose_outlier.rolling(50).median()
adjClose_outlier.plot(color = 'blue')
MA20.plot(color = 'green')
MA50.plot(color = 'red')
plt.fill_between(adjClose_outlier.index, MA20, MA50, where = MA20 > MA50, color = 'g', alpha = 0.3)
plt.fill_between(adjClose_outlier.index, MA20, MA50, where = MA20 < MA50, color = 'r', alpha = 0.3)
plt.legend(['Adjusted Close Prices', 'ME20', 'ME50', 'Buy', 'Sell'])
plt.title('Moving Median Crossover Strategy for DJI with Outliers', fontname="arial", fontsize=16, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=14)
plt.ylabel('Price', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=14)
plt.yticks(fontname="arial", fontsize=14)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(8, 6)
plt.savefig('4_4_2o.pdf')
# dates = ['2018-05-14', '2018-09-14', '2018-12-14', '2019-01-14', '2018-06-21', '2018-10-10', '2018-07-23']
dates = ['2018-05-14', '2018-09-14', '2018-12-14', '2019-01-14']
max_value = apple_data['Adj Close'].max()
for date in dates:
apple_data['Adj Close'][date] = max_value * 1.2
apple_data['Ret'] = apple_data['Adj Close'].pct_change()
apple_data['Action'] = np.ones(len(apple_data)) * np.nan
apple_data['20 MA'] = apple_data['Adj Close'].rolling(20).median().dropna()
apple_data['50 MA'] = apple_data['Adj Close'].rolling(50).median().dropna()
apple_data.loc[apple_data['20 MA'] >= apple_data['50 MA'], 'Action'] = 1
apple_data.loc[apple_data['20 MA'] < apple_data['50 MA'], 'Action'] = -1
apple_data['cumulative'] = np.cumsum(apple_data['Ret'] * apple_data['Action'])
apple_data['cumulative'].plot(color = 'blue')
plt.title('Cumulative Return of DJI with Outliers using Moving Median Crossover Strategy', fontname="arial", fontsize=14, fontweight="bold")
plt.xlabel('Date', fontname="arial", fontsize=14)
plt.ylabel('Cumulative Returns', fontname="arial", fontsize=14)
plt.xticks(fontname="arial", fontsize=14)
plt.yticks(fontname="arial", fontsize=14)
plt.grid(which='major', linestyle='-', linewidth='0.5', color='#666666')
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
fig = plt.gcf()
fig.set_size_inches(8, 6)
plt.savefig('4_4_2p.pdf')
<ipython-input-101-0723e5fff1d0>:7: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy apple_data['Adj Close'][date] = max_value * 1.2 /opt/anaconda3/lib/python3.8/site-packages/pandas/core/indexing.py:670: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy iloc._setitem_with_indexer(indexer, value)